NUMBERS-54: Create module "commons-numbers-complex-streams". Class "ComplexUtils" moved over to the new module.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/40418955 Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/40418955 Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/40418955 Branch: refs/heads/master Commit: 40418955926ced67c155cf03009a0f4b54c8440b Parents: 8e0af85 Author: Gilles Sadowski <[email protected]> Authored: Sat May 19 14:42:42 2018 +0200 Committer: Gilles Sadowski <[email protected]> Committed: Sat May 19 14:42:42 2018 +0200 ---------------------------------------------------------------------- commons-numbers-complex-streams/LICENSE.txt | 201 ++ commons-numbers-complex-streams/NOTICE.txt | 6 + commons-numbers-complex-streams/README.md | 105 ++ commons-numbers-complex-streams/pom.xml | 60 + .../numbers/complex/streams/ComplexUtils.java | 1742 ++++++++++++++++++ .../numbers/complex/streams/package-info.java | 20 + .../src/site/resources/profile.jacoco | 17 + .../complex/streams/ComplexUtilsTest.java | 476 +++++ .../numbers/complex/streams/TestUtils.java | 410 +++++ .../commons/numbers/complex/ComplexUtils.java | 1740 ----------------- .../commons/numbers/complex/ComplexTest.java | 5 +- .../numbers/complex/ComplexUtilsTest.java | 476 ----- pom.xml | 6 + 13 files changed, 3045 insertions(+), 2219 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/40418955/commons-numbers-complex-streams/LICENSE.txt ---------------------------------------------------------------------- diff --git a/commons-numbers-complex-streams/LICENSE.txt b/commons-numbers-complex-streams/LICENSE.txt new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/commons-numbers-complex-streams/LICENSE.txt @@ -0,0 +1,201 @@ + 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. http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/40418955/commons-numbers-complex-streams/NOTICE.txt ---------------------------------------------------------------------- diff --git a/commons-numbers-complex-streams/NOTICE.txt b/commons-numbers-complex-streams/NOTICE.txt new file mode 100644 index 0000000..9091baa --- /dev/null +++ b/commons-numbers-complex-streams/NOTICE.txt @@ -0,0 +1,6 @@ +Apache Commons Numbers +Copyright 2001-2017 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/40418955/commons-numbers-complex-streams/README.md ---------------------------------------------------------------------- diff --git a/commons-numbers-complex-streams/README.md b/commons-numbers-complex-streams/README.md new file mode 100644 index 0000000..30a03eb --- /dev/null +++ b/commons-numbers-complex-streams/README.md @@ -0,0 +1,105 @@ +<!--- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT 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 FILE IS GENERATED BY THE COMMONS BUILD PLUGIN ****| + |**** DO NOT EDIT DIRECTLY ****| + |**** ****| + +======================================================================+ + | TEMPLATE FILE: readme-md-template.md | + | commons-build-plugin/trunk/src/main/resources/commons-xdoc-templates | + +======================================================================+ + | | + | 1) Re-generate using: mvn commons:readme-md | + | | + | 2) Set the following properties in the component's pom: | + | - commons.componentid (required, alphabetic, lower case) | + | - commons.release.version (required) | + | | + | 3) Example Properties | + | | + | <properties> | + | <commons.componentid>math</commons.componentid> | + | <commons.release.version>1.2</commons.release.version> | + | </properties> | + | | + +======================================================================+ +---> +Apache Commons Numbers Complex Streams +=================== + +[](https://travis-ci.org/apache/commons-numbers-complex-streams) +[](https://coveralls.io/r/apache/commons-numbers-complex-streams) +[](https://maven-badges.herokuapp.com/maven-central/org.apache.commons/commons-numbers-complex-streams/) +[](http://www.apache.org/licenses/LICENSE-2.0.html) + +Arrays, streams and collections of complex numbers. + +Documentation +------------- + +More information can be found on the [Apache Commons Numbers Complex Streams homepage](https://commons.apache.org/proper/commons-numbers). +The [JavaDoc](https://commons.apache.org/proper/commons-numbers/javadocs/api-release) can be browsed. +Questions related to the usage of Apache Commons Numbers Complex Streams should be posted to the [user mailing list][ml]. + +Where can I get the latest release? +----------------------------------- +You can download source and binaries from our [download page](https://commons.apache.org/proper/commons-numbers/download_numbers.cgi). + +Alternatively you can pull it from the central Maven repositories: + +```xml +<dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-complex-streams</artifactId> + <version>1.0</version> +</dependency> +``` + +Contributing +------------ + +We accept Pull Requests via GitHub. The [developer mailing list][ml] is the main channel of communication for contributors. +There are some guidelines which will make applying PRs easier for us: ++ No tabs! Please use spaces for indentation. ++ Respect the code style. ++ Create minimal diffs - disable on save actions like reformat source code or organize imports. If you feel the source code should be reformatted create a separate PR for this change. ++ Provide JUnit tests for your changes and make sure your changes don't break any existing tests by running ```mvn clean test```. + +If you plan to contribute on a regular basis, please consider filing a [contributor license agreement](https://www.apache.org/licenses/#clas). +You can learn more about contributing via GitHub in our [contribution guidelines](CONTRIBUTING.md). + +License +------- +This code is under the [Apache Licence v2](https://www.apache.org/licenses/LICENSE-2.0). + +See the `NOTICE.txt` file for required notices and attributions. + +Donations +--------- +You like Apache Commons Numbers Complex Streams? Then [donate back to the ASF](https://www.apache.org/foundation/contributing.html) to support the development. + +Additional Resources +-------------------- + ++ [Apache Commons Homepage](https://commons.apache.org/) ++ [Apache Issue Tracker (JIRA)](https://issues.apache.org/jira/browse/NUMBERS) ++ [Apache Commons Twitter Account](https://twitter.com/ApacheCommons) ++ `#apache-commons` IRC channel on `irc.freenode.org` + +[ml]:https://commons.apache.org/mail-lists.html http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/40418955/commons-numbers-complex-streams/pom.xml ---------------------------------------------------------------------- diff --git a/commons-numbers-complex-streams/pom.xml b/commons-numbers-complex-streams/pom.xml new file mode 100644 index 0000000..f388537 --- /dev/null +++ b/commons-numbers-complex-streams/pom.xml @@ -0,0 +1,60 @@ +<?xml version="1.0"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" + xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-parent</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-complex-streams</artifactId> + <version>1.0-SNAPSHOT</version> + <name>Apache Commons Numbers Complex Streams</name> + + <description>Arrays, streams and collections of complex numbers.</description> + + <properties> + <!-- This value must reflect the current name of the base package. --> + <commons.osgi.symbolicName>org.apache.commons.numbers.complex.streams</commons.osgi.symbolicName> + <!-- OSGi --> + <commons.osgi.export>org.apache.commons.numbers.complex.streams</commons.osgi.export> + <!-- Workaround to avoid duplicating config files. --> + <numbers.parent.dir>${basedir}/..</numbers.parent.dir> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-numbers-complex</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-math3</artifactId> + <version>3.6.1</version> + <scope>test</scope> + </dependency> + + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/40418955/commons-numbers-complex-streams/src/main/java/org/apache/commons/numbers/complex/streams/ComplexUtils.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex-streams/src/main/java/org/apache/commons/numbers/complex/streams/ComplexUtils.java b/commons-numbers-complex-streams/src/main/java/org/apache/commons/numbers/complex/streams/ComplexUtils.java new file mode 100644 index 0000000..5c0d7d1 --- /dev/null +++ b/commons-numbers-complex-streams/src/main/java/org/apache/commons/numbers/complex/streams/ComplexUtils.java @@ -0,0 +1,1742 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.numbers.complex.streams; + +import org.apache.commons.numbers.complex.Complex; + +/** + * Static implementations of common {@link Complex} utilities functions. + */ +public class ComplexUtils { + + /** + * Utility class. + */ + private ComplexUtils() {} + + /** + * Creates a complex number from the given polar representation. + * <p> + * If either {@code r} or {@code theta} is NaN, or {@code theta} is + * infinite, {@link Complex#NAN} is returned. + * <p> + * If {@code r} is infinite and {@code theta} is finite, infinite or NaN + * values may be returned in parts of the result, following the rules for + * double arithmetic. + * + * <pre> + * Examples: + * {@code + * polar2Complex(INFINITY, \(\pi\)) = INFINITY + INFINITY i + * polar2Complex(INFINITY, 0) = INFINITY + NaN i + * polar2Complex(INFINITY, \(-\frac{\pi}{4}\)) = INFINITY - INFINITY i + * polar2Complex(INFINITY, \(5\frac{\pi}{4}\)) = -INFINITY - INFINITY i } + * </pre> + * + * @param r the modulus of the complex number to create + * @param theta the argument of the complex number to create + * @return {@code Complex} + * @since 1.1 + */ + public static Complex polar2Complex(double r, double theta) { + if (r < 0) { + throw new NegativeModulusException(r); + } + return Complex.ofCartesian(r * Math.cos(theta), r * Math.sin(theta)); + } + + /** + * Creates {@code Complex[]} array given {@code double[]} arrays of r and + * theta. + * + * @param r {@code double[]} of moduli + * @param theta {@code double[]} of arguments + * @return {@code Complex[]} + * @since 1.0 + */ + public static Complex[] polar2Complex(double[] r, double[] theta) { + final int length = r.length; + final Complex[] c = new Complex[length]; + for (int x = 0; x < length; x++) { + if (r[x] < 0) { + throw new NegativeModulusException(r[x]); + } + c[x] = Complex.ofCartesian(r[x] * Math.cos(theta[x]), r[x] * Math.sin(theta[x])); + } + return c; + } + + /** + * Creates {@code Complex[][]} array given {@code double[][]} arrays of r + * and theta. + * + * @param r {@code double[]} of moduli + * @param theta {@code double[]} of arguments + * @return {@code Complex[][]} + * @since 1.0 + */ + public static Complex[][] polar2Complex(double[][] r, double[][] theta) { + final int length = r.length; + final Complex[][] c = new Complex[length][]; + for (int x = 0; x < length; x++) { + c[x] = polar2Complex(r[x], theta[x]); + } + return c; + } + + /** + * Creates {@code Complex[][][]} array given {@code double[][][]} arrays of + * r and theta. + * + * @param r array of moduli + * @param theta array of arguments + * @return {@code Complex} + * @since 1.0 + */ + public static Complex[][][] polar2Complex(double[][][] r, double[][][] theta) { + final int length = r.length; + final Complex[][][] c = new Complex[length][][]; + for (int x = 0; x < length; x++) { + c[x] = polar2Complex(r[x], theta[x]); + } + return c; + } + + /** + * Returns double from array {@code real[]} at entry {@code index} as a + * {@code Complex}. + * + * @param real array of real numbers + * @param index location in the array + * @return {@code Complex}. + * + * @since 1.0 + */ + public static Complex extractComplexFromRealArray(double[] real, int index) { + return Complex.ofReal(real[index]); + } + + /** + * Returns float from array {@code real[]} at entry {@code index} as a + * {@code Complex}. + * + * @param real array of real numbers + * @param index location in the array + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex extractComplexFromRealArray(float[] real, int index) { + return Complex.ofReal(real[index]); + } + + /** + * Returns double from array {@code imaginary[]} at entry {@code index} as a + * {@code Complex}. + * + * @param imaginary array of imaginary numbers + * @param index location in the array + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex extractComplexFromImaginaryArray(double[] imaginary, int index) { + return Complex.ofCartesian(0, imaginary[index]); + } + + /** + * Returns float from array {@code imaginary[]} at entry {@code index} as a + * {@code Complex}. + * + * @param imaginary array of imaginary numbers + * @param index location in the array + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex extractComplexFromImaginaryArray(float[] imaginary, int index) { + return Complex.ofCartesian(0, imaginary[index]); + } + + /** + * Returns real component of Complex from array {@code Complex[]} at entry + * {@code index} as a {@code double}. + * + * @param complex array of complex numbers + * @param index location in the array + * @return {@code double}. + * + * @since 1.0 + */ + public static double extractRealFromComplexArray(Complex[] complex, int index) { + return complex[index].getReal(); + } + + /** + * Returns real component of array {@code Complex[]} at entry {@code index} + * as a {@code float}. + * + * @param complex array of complex numbers + * @param index location in the array + * @return {@code float}. + * + * @since 1.0 + */ + public static float extractRealFloatFromComplexArray(Complex[] complex, int index) { + return (float) complex[index].getReal(); + } + + /** + * Returns imaginary component of Complex from array {@code Complex[]} at + * entry {@code index} as a {@code double}. + * + * @param complex array of complex numbers + * @param index location in the array + * @return {@code double}. + * + * @since 1.0 + */ + public static double extractImaginaryFromComplexArray(Complex[] complex, int index) { + return complex[index].getImaginary(); + } + + /** + * Returns imaginary component of array {@code Complex[]} at entry + * {@code index} as a {@code float}. + * + * @param complex array of complex numbers + * @param index location in the array + * @return {@code float}. + * + * @since 1.0 + */ + public static float extractImaginaryFloatFromComplexArray(Complex[] complex, int index) { + return (float) complex[index].getImaginary(); + } + + /** + * Returns a Complex object from interleaved {@code double[]} array at entry + * {@code index}. + * + * @param d array of interleaved complex numbers alternating real and imaginary values + * @param index location in the array This is the location by complex number, e.g. index number 5 in the array will return {@code Complex.ofCartesian(d[10], d[11])} + * @return {@code Complex}. + * + * @since 1.0 + */ + public static Complex extractComplexFromInterleavedArray(double[] d, int index) { + return Complex.ofCartesian(d[index * 2], d[index * 2 + 1]); + } + + /** + * Returns a Complex object from interleaved {@code float[]} array at entry + * {@code index}. + * + * @param f float array of interleaved complex numbers alternating real and imaginary values + * @param index location in the array This is the location by complex number, e.g. index number 5 in the {@code float[]} array will return new {@code Complex(d[10], d[11])} + * @return {@code Complex}. + * + * @since 1.0 + */ + public static Complex extractComplexFromInterleavedArray(float[] f, int index) { + return Complex.ofCartesian(f[index * 2], f[index * 2 + 1]); + } + + /** + * Returns values of Complex object from array {@code Complex[]} at entry + * {@code index} as a size 2 {@code double} of the form {real, imag}. + * + * @param complex array of complex numbers + * @param index location in the array + * @return size 2 array. + * + * @since 1.0 + */ + public static double[] extractInterleavedFromComplexArray(Complex[] complex, int index) { + return new double[] { complex[index].getReal(), complex[index].getImaginary() }; + } + + /** + * Returns Complex object from array {@code Complex[]} at entry + * {@code index} as a size 2 {@code float} of the form {real, imag}. + * + * @param complex {@code Complex} array + * @param index location in the array + * @return size 2 {@code float[]}. + * + * @since 1.0 + */ + public static float[] extractInterleavedFloatFromComplexArray(Complex[] complex, int index) { + return new float[] { (float) complex[index].getReal(), (float) complex[index].getImaginary() }; + } + + /** + * Converts a {@code double[]} array to a {@code Complex[]} array. + * + * @param real array of numbers to be converted to their {@code Complex} equivalent + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex[] real2Complex(double[] real) { + int index = 0; + final Complex c[] = new Complex[real.length]; + for (double d : real) { + c[index] = Complex.ofReal(d); + index++; + } + return c; + } + + /** + * Converts a {@code float[]} array to a {@code Complex[]} array. + * + * @param real array of numbers to be converted to their {@code Complex} equivalent + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex[] real2Complex(float[] real) { + int index = 0; + final Complex c[] = new Complex[real.length]; + for (float d : real) { + c[index] = Complex.ofReal(d); + index++; + } + return c; + } + + /** + * Converts a 2D real {@code double[][]} array to a 2D {@code Complex[][]} + * array. + * + * @param d 2D array + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] real2Complex(double[][] d) { + final int w = d.length; + final Complex[][] c = new Complex[w][]; + for (int n = 0; n < w; n++) { + c[n] = ComplexUtils.real2Complex(d[n]); + } + return c; + } + + /** + * Converts a 2D real {@code float[][]} array to a 2D {@code Complex[][]} + * array. + * + * @param d 2D array + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] real2Complex(float[][] d) { + final int w = d.length; + final Complex[][] c = new Complex[w][]; + for (int n = 0; n < w; n++) { + c[n] = ComplexUtils.real2Complex(d[n]); + } + return c; + } + + /** + * Converts a 3D real {@code double[][][]} array to a {@code Complex [][][]} + * array. + * + * @param d 3D complex interleaved array + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] real2Complex(double[][][] d) { + final int w = d.length; + final Complex[][][] c = new Complex[w][][]; + for (int x = 0; x < w; x++) { + c[x] = ComplexUtils.real2Complex(d[x]); + } + return c; + } + + /** + * Converts a 3D real {@code float[][][]} array to a {@code Complex [][][]} + * array. + * + * @param d 3D complex interleaved array + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] real2Complex(float[][][] d) { + final int w = d.length; + final Complex[][][] c = new Complex[w][][]; + for (int x = 0; x < w; x++) { + c[x] = ComplexUtils.real2Complex(d[x]); + } + return c; + } + + /** + * Converts a 4D real {@code double[][][][]} array to a {@code Complex [][][][]} + * array. + * + * @param d 4D complex interleaved array + * @return 4D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][][] real2Complex(double[][][][] d) { + final int w = d.length; + final Complex[][][][] c = new Complex[w][][][]; + for (int x = 0; x < w; x++) { + c[x] = ComplexUtils.real2Complex(d[x]); + } + return c; + } + + /** + * Converts real component of {@code Complex[]} array to a {@code double[]} + * array. + * + * @param c {@code Complex} array + * @return array of the real component + * + * @since 1.0 + */ + public static double[] complex2Real(Complex[] c) { + int index = 0; + final double d[] = new double[c.length]; + for (Complex cc : c) { + d[index] = cc.getReal(); + index++; + } + return d; + } + + /** + * Converts real component of {@code Complex[]} array to a {@code float[]} + * array. + * + * @param c {@code Complex} array + * @return {@code float[]} array of the real component + * + * @since 1.0 + */ + public static float[] complex2RealFloat(Complex[] c) { + int index = 0; + final float f[] = new float[c.length]; + for (Complex cc : c) { + f[index] = (float) cc.getReal(); + index++; + } + return f; + } + + /** + * Converts real component of a 2D {@code Complex[][]} array to a 2D + * {@code double[][]} array. + * + * @param c 2D {@code Complex} array + * @return {@code double[][]} of real component + * @since 1.0 + */ + public static double[][] complex2Real(Complex[][] c) { + final int length = c.length; + double[][] d = new double[length][]; + for (int n = 0; n < length; n++) { + d[n] = complex2Real(c[n]); + } + return d; + } + + /** + * Converts real component of a 2D {@code Complex[][]} array to a 2D + * {@code float[][]} array. + * + * @param c 2D {@code Complex} array + * @return {@code float[][]} of real component + * @since 1.0 + */ + public static float[][] complex2RealFloat(Complex[][] c) { + final int length = c.length; + float[][] f = new float[length][]; + for (int n = 0; n < length; n++) { + f[n] = complex2RealFloat(c[n]); + } + return f; + } + + /** + * Converts real component of a 3D {@code Complex[][][]} array to a 3D + * {@code double[][][]} array. + * + * @param c 3D complex interleaved array + * @return array of real component + * + * @since 1.0 + */ + public static double[][][] complex2Real(Complex[][][] c) { + final int length = c.length; + double[][][] d = new double[length][][]; + for (int n = 0; n < length; n++) { + d[n] = complex2Real(c[n]); + } + return d; + } + + /** + * Converts real component of a 3D {@code Complex[][][]} array to a 3D + * {@code float[][][]} array. + * + * @param c 3D {@code Complex} array + * @return {@code float[][][]} of real component + * @since 1.0 + */ + public static float[][][] complex2RealFloat(Complex[][][] c) { + final int length = c.length; + float[][][] f = new float[length][][]; + for (int n = 0; n < length; n++) { + f[n] = complex2RealFloat(c[n]); + } + return f; + } + + /** + * Converts real component of a 4D {@code Complex[][][][]} array to a 4D + * {@code double[][][][]} array. + * + * @param c 4D complex interleaved array + * @return array of real component + * + * @since 1.0 + */ + public static double[][][][] complex2Real(Complex[][][][] c) { + final int length = c.length; + double[][][][] d = new double[length][][][]; + for (int n = 0; n < length; n++) { + d[n] = complex2Real(c[n]); + } + return d; + } + + /** + * Converts real component of a 4D {@code Complex[][][][]} array to a 4D + * {@code float[][][][]} array. + * + * @param c 4D {@code Complex} array + * @return {@code float[][][][]} of real component + * @since 1.0 + */ + public static float[][][][] complex2RealFloat(Complex[][][][] c) { + final int length = c.length; + float[][][][] f = new float[length][][][]; + for (int n = 0; n < length; n++) { + f[n] = complex2RealFloat(c[n]); + } + return f; + } + + /** + * Converts a {@code double[]} array to an imaginary {@code Complex[]} + * array. + * + * @param imaginary array of numbers to be converted to their {@code Complex} equivalent + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex[] imaginary2Complex(double[] imaginary) { + int index = 0; + final Complex c[] = new Complex[imaginary.length]; + for (double d : imaginary) { + c[index] = Complex.ofCartesian(0, d); + index++; + } + return c; + } + + /** + * Converts a {@code float[]} array to an imaginary {@code Complex[]} array. + * + * @param imaginary array of numbers to be converted to their {@code Complex} equivalent + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex[] imaginary2Complex(float[] imaginary) { + int index = 0; + final Complex c[] = new Complex[imaginary.length]; + for (float d : imaginary) { + c[index] = Complex.ofCartesian(0, d); + index++; + } + return c; + } + + /** + * Converts a 2D imaginary array {@code double[][]} to a 2D + * {@code Complex[][]} array. + * + * @param i 2D array + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] imaginary2Complex(double[][] i) { + int w = i.length; + Complex[][] c = new Complex[w][]; + for (int n = 0; n < w; n++) { + c[n] = ComplexUtils.imaginary2Complex(i[n]); + } + return c; + } + + /** + * Converts a 3D imaginary array {@code double[][][]} to a {@code Complex[]} + * array. + * + * @param i 3D complex imaginary array + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] imaginary2Complex(double[][][] i) { + int w = i.length; + Complex[][][] c = new Complex[w][][]; + for (int n = 0; n < w; n++) { + c[n] = ComplexUtils.imaginary2Complex(i[n]); + } + return c; + } + + /** + * Converts a 4D imaginary array {@code double[][][][]} to a 4D {@code Complex[][][][]} + * array. + * + * @param i 4D complex imaginary array + * @return 4D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][][] imaginary2Complex(double[][][][] i) { + int w = i.length; + Complex[][][][] c = new Complex[w][][][]; + for (int n = 0; n < w; n++) { + c[n] = ComplexUtils.imaginary2Complex(i[n]); + } + return c; + } + + /** + * Converts imaginary part of a {@code Complex[]} array to a + * {@code double[]} array. + * + * @param c {@code Complex} array. + * @return array of the imaginary component + * + * @since 1.0 + */ + public static double[] complex2Imaginary(Complex[] c) { + int index = 0; + final double i[] = new double[c.length]; + for (Complex cc : c) { + i[index] = cc.getImaginary(); + index++; + } + return i; + } + + /** + * Converts imaginary component of a {@code Complex[]} array to a + * {@code float[]} array. + * + * @param c {@code Complex} array. + * @return {@code float[]} array of the imaginary component + * + * @since 1.0 + */ + public static float[] complex2ImaginaryFloat(Complex[] c) { + int index = 0; + final float f[] = new float[c.length]; + for (Complex cc : c) { + f[index] = (float) cc.getImaginary(); + index++; + } + return f; + } + + /** + * Converts imaginary component of a 2D {@code Complex[][]} array to a 2D + * {@code double[][]} array. + * + * @param c 2D {@code Complex} array + * @return {@code double[][]} of imaginary component + * @since 1.0 + */ + public static double[][] complex2Imaginary(Complex[][] c) { + final int length = c.length; + double[][] i = new double[length][]; + for (int n = 0; n < length; n++) { + i[n] = complex2Imaginary(c[n]); + } + return i; + } + + /** + * Converts imaginary component of a 2D {@code Complex[][]} array to a 2D + * {@code float[][]} array. + * + * @param c 2D {@code Complex} array + * @return {@code float[][]} of imaginary component + * @since 1.0 + */ + public static float[][] complex2ImaginaryFloat(Complex[][] c) { + final int length = c.length; + float[][] f = new float[length][]; + for (int n = 0; n < length; n++) { + f[n] = complex2ImaginaryFloat(c[n]); + } + return f; + } + + /** + * Converts imaginary component of a 3D {@code Complex[][][]} array to a 3D + * {@code double[][][]} array. + * + * @param c 3D complex interleaved array + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static double[][][] complex2Imaginary(Complex[][][] c) { + final int length = c.length; + double[][][] i = new double[length][][]; + for (int n = 0; n < length; n++) { + i[n] = complex2Imaginary(c[n]); + } + return i; + } + + /** + * Converts imaginary component of a 3D {@code Complex[][][]} array to a 3D + * {@code float[][][]} array. + * + * @param c 3D {@code Complex} array + * @return {@code float[][][]} of imaginary component + * @since 1.0 + */ + public static float[][][] complex2ImaginaryFloat(Complex[][][] c) { + final int length = c.length; + float[][][] f = new float[length][][]; + for (int n = 0; n < length; n++) { + f[n] = complex2ImaginaryFloat(c[n]); + } + return f; + } + + /** + * Converts imaginary component of a 4D {@code Complex[][][][]} array to a 4D + * {@code double[][][][]} array. + * + * @param c 4D complex interleaved array + * @return 4D {@code Complex} array + * + * @since 1.0 + */ + public static double[][][][] complex2Imaginary(Complex[][][][] c) { + final int length = c.length; + double[][][][] i = new double[length][][][]; + for (int n = 0; n < length; n++) { + i[n] = complex2Imaginary(c[n]); + } + return i; + } + + /** + * Converts imaginary component of a 4D {@code Complex[][][][]} array to a 4D + * {@code float[][][][]} array. + * + * @param c 4D {@code Complex} array + * @return {@code float[][][][]} of imaginary component + * @since 1.0 + */ + public static float[][][][] complex2ImaginaryFloat(Complex[][][][] c) { + final int length = c.length; + float[][][][] f = new float[length][][][]; + for (int n = 0; n < length; n++) { + f[n] = complex2ImaginaryFloat(c[n]); + } + return f; + } + + // INTERLEAVED METHODS + + /** + * Converts a complex interleaved {@code double[]} array to a + * {@code Complex[]} array + * + * @param interleaved array of numbers to be converted to their {@code Complex} equivalent + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex[] interleaved2Complex(double[] interleaved) { + final int length = interleaved.length / 2; + final Complex c[] = new Complex[length]; + for (int n = 0; n < length; n++) { + c[n] = Complex.ofCartesian(interleaved[n * 2], interleaved[n * 2 + 1]); + } + return c; + } + + /** + * Converts a complex interleaved {@code float[]} array to a + * {@code Complex[]} array + * + * @param interleaved float[] array of numbers to be converted to their {@code Complex} equivalent + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex[] interleaved2Complex(float[] interleaved) { + final int length = interleaved.length / 2; + final Complex c[] = new Complex[length]; + for (int n = 0; n < length; n++) { + c[n] = Complex.ofCartesian(interleaved[n * 2], interleaved[n * 2 + 1]); + } + return c; + } + + /** + * Converts a {@code Complex[]} array to an interleaved complex + * {@code double[]} array + * + * @param c Complex array + * @return complex interleaved array alternating real and + * imaginary values + * + * @since 1.0 + */ + public static double[] complex2Interleaved(Complex[] c) { + int index = 0; + final double i[] = new double[c.length * 2]; + for (Complex cc : c) { + int real = index * 2; + int imag = index * 2 + 1; + i[real] = cc.getReal(); + i[imag] = cc.getImaginary(); + index++; + } + return i; + } + + /** + * Converts a {@code Complex[]} array to an interleaved complex + * {@code float[]} array + * + * @param c Complex array + * @return complex interleaved {@code float[]} alternating real and + * imaginary values + * + * @since 1.0 + */ + public static float[] complex2InterleavedFloat(Complex[] c) { + int index = 0; + final float f[] = new float[c.length * 2]; + for (Complex cc : c) { + int real = index * 2; + int imag = index * 2 + 1; + f[real] = (float) cc.getReal(); + f[imag] = (float) cc.getImaginary(); + index++; + } + return f; + } + + /** + * Converts a 2D {@code Complex[][]} array to an interleaved complex + * {@code double[][]} array. + * + * @param c 2D Complex array + * @param interleavedDim Depth level of the array to interleave + * @return complex interleaved array alternating real and + * imaginary values + * + * @since 1.0 + */ + public static double[][] complex2Interleaved(Complex[][] c, int interleavedDim) { + if (interleavedDim > 1 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + final int w = c.length; + final int h = c[0].length; + double[][] i; + if (interleavedDim == 0) { + i = new double[2 * w][h]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + i[x * 2][y] = c[x][y].getReal(); + i[x * 2 + 1][y] = c[x][y].getImaginary(); + } + } + } else { + i = new double[w][2 * h]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + i[x][y * 2] = c[x][y].getReal(); + i[x][y * 2 + 1] = c[x][y].getImaginary(); + } + } + } + return i; + } + + /** + * Converts a 2D {@code Complex[][]} array to an interleaved complex + * {@code double[][]} array. The second d level of the array is assumed + * to be interleaved. + * + * @param c 2D Complex array + * @return complex interleaved array alternating real and + * imaginary values + * + * @since 1.0 + */ + public static double[][] complex2Interleaved(Complex[][] c) { + return complex2Interleaved(c, 1); + } + + /** + * Converts a 3D {@code Complex[][][]} array to an interleaved complex + * {@code double[][][]} array. + * + * @param c 3D Complex array + * @param interleavedDim Depth level of the array to interleave + * @return complex interleaved array alternating real and + * imaginary values + * + * @since 1.0 + */ + public static double[][][] complex2Interleaved(Complex[][][] c, int interleavedDim) { + if (interleavedDim > 2 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + int w = c.length; + int h = c[0].length; + int d = c[0][0].length; + double[][][] i; + if (interleavedDim == 0) { + i = new double[2 * w][h][d]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + i[x * 2][y][z] = c[x][y][z].getReal(); + i[x * 2 + 1][y][z] = c[x][y][z].getImaginary(); + } + } + } + } else if (interleavedDim == 1) { + i = new double[w][2 * h][d]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + i[x][y * 2][z] = c[x][y][z].getReal(); + i[x][y * 2 + 1][z] = c[x][y][z].getImaginary(); + } + } + } + } else { + i = new double[w][h][2 * d]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + i[x][y][z * 2] = c[x][y][z].getReal(); + i[x][y][z * 2 + 1] = c[x][y][z].getImaginary(); + } + } + } + } + return i; + } + + /** + * Converts a 4D {@code Complex[][][][]} array to an interleaved complex + * {@code double[][][][]} array. + * + * @param c 4D Complex array + * @param interleavedDim Depth level of the array to interleave + * @return complex interleaved array alternating real and + * imaginary values + * + * @since 1.0 + */ + public static double[][][][] complex2Interleaved(Complex[][][][] c, int interleavedDim) { + if (interleavedDim > 3 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + int w = c.length; + int h = c[0].length; + int d = c[0][0].length; + int v = c[0][0][0].length; + double[][][][] i; + if (interleavedDim == 0) { + i = new double[2 * w][h][d][v]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + for (int t = 0; t > v; t++) { + i[x * 2][y][z][t] = c[x][y][z][t].getReal(); + i[x * 2 + 1][y][z][t] = c[x][y][z][t].getImaginary(); + } + } + } + } + } else if (interleavedDim == 1) { + i = new double[w][2 * h][d][v]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + for (int t = 0; t > v; t++) { + i[x][y * 2][z][t] = c[x][y][z][t].getReal(); + i[x][y * 2 + 1][z][t] = c[x][y][z][t].getImaginary(); + } + } + } + } + } else if (interleavedDim == 2) { + i = new double[w][h][2 * d][v]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + for (int t = 0; t > v; t++) { + i[x][y][z * 2][t] = c[x][y][z][t].getReal(); + i[x][y][z * 2 + 1][t] = c[x][y][z][t].getImaginary(); + } + } + } + } + } else { + i = new double[w][h][d][2 * v]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + for (int t = 0; t > v; t++) { + i[x][y][z][t * 2] = c[x][y][z][t].getReal(); + i[x][y][z][t * 2 + 1] = c[x][y][z][t].getImaginary(); + } + } + } + } + } + return i; + } + + /** + * Converts a 3D {@code Complex[][][]} array to an interleaved complex + * {@code double[][][]} array. The third level of the array is + * interleaved. + * + * @param c 3D Complex array + * @return complex interleaved array alternating real and + * imaginary values + * + * @since 1.0 + */ + public static double[][][] complex2Interleaved(Complex[][][] c) { + return complex2Interleaved(c, 2); + } + + /** + * Converts a 4D {@code Complex[][][][]} array to an interleaved complex + * {@code double[][][][]} array. The fourth level of the array is + * interleaved. + * + * @param c 4D Complex array + * @return complex interleaved array alternating real and + * imaginary values + * + * @since 1.0 + */ + public static double[][][][] complex2Interleaved(Complex[][][][] c) { + return complex2Interleaved(c, 3); + } + + /** + * Converts a 2D {@code Complex[][]} array to an interleaved complex + * {@code float[][]} array. + * + * @param c 2D Complex array + * @param interleavedDim Depth level of the array to interleave + * @return complex interleaved {@code float[][]} alternating real and + * imaginary values + * + * @since 1.0 + */ + public static float[][] complex2InterleavedFloat(Complex[][] c, int interleavedDim) { + if (interleavedDim > 1 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + final int w = c.length; + final int h = c[0].length; + float[][] i; + if (interleavedDim == 0) { + i = new float[2 * w][h]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + i[x * 2][y] = (float) c[x][y].getReal(); + i[x * 2 + 1][y] = (float) c[x][y].getImaginary(); + } + } + } else { + i = new float[w][2 * h]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + i[x][y * 2] = (float) c[x][y].getReal(); + i[x][y * 2 + 1] = (float) c[x][y].getImaginary(); + } + } + } + return i; + } + + /** + * Converts a 2D {@code Complex[][]} array to an interleaved complex + * {@code float[][]} array. The second d level of the array is assumed + * to be interleaved. + * + * @param c 2D Complex array + * + * @return complex interleaved {@code float[][]} alternating real and + * imaginary values + * + * @since 1.0 + */ + public static float[][] complex2InterleavedFloat(Complex[][] c) { + return complex2InterleavedFloat(c, 1); + } + + /** + * Converts a 3D {@code Complex[][][]} array to an interleaved complex + * {@code float[][][]} array. + * + * @param c 3D Complex array + * @param interleavedDim Depth level of the array to interleave + * @return complex interleaved {@code float[][][]} alternating real and + * imaginary values + * + * @since 1.0 + */ + public static float[][][] complex2InterleavedFloat(Complex[][][] c, int interleavedDim) { + if (interleavedDim > 2 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + final int w = c.length; + final int h = c[0].length; + final int d = c[0][0].length; + float[][][] i; + if (interleavedDim == 0) { + i = new float[2 * w][h][d]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + i[x * 2][y][z] = (float) c[x][y][z].getReal(); + i[x * 2 + 1][y][z] = (float) c[x][y][z].getImaginary(); + } + } + } + } else if (interleavedDim == 1) { + i = new float[w][2 * h][d]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + i[x][y * 2][z] = (float) c[x][y][z].getReal(); + i[x][y * 2 + 1][z] = (float) c[x][y][z].getImaginary(); + } + } + } + } else { + i = new float[w][h][2 * d]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + i[x][y][z * 2] = (float) c[x][y][z].getReal(); + i[x][y][z * 2 + 1] = (float) c[x][y][z].getImaginary(); + } + } + } + } + return i; + } + + /** + * Converts a 3D {@code Complex[][][]} array to an interleaved complex + * {@code float[][][]} array. The third d level of the array is + * interleaved. + * + * @param c 2D Complex array + * + * @return complex interleaved {@code float[][][]} alternating real and + * imaginary values + * + * @since 1.0 + */ + public static float[][][] complex2InterleavedFloat(Complex[][][] c) { + return complex2InterleavedFloat(c, 2); + } + + /** + * Converts a 2D interleaved complex {@code double[][]} array to a + * {@code Complex[][]} array. + * + * @param i 2D complex interleaved array + * @param interleavedDim Depth level of the array to interleave + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] interleaved2Complex(double[][] i, int interleavedDim) { + if (interleavedDim > 1 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + final int w = i.length; + final int h = i[0].length; + Complex[][] c; + if (interleavedDim == 0) { + c = new Complex[w / 2][h]; + for (int x = 0; x < w / 2; x++) { + for (int y = 0; y < h; y++) { + c[x][y] = Complex.ofCartesian(i[x * 2][y], i[x * 2 + 1][y]); + } + } + } else { + c = new Complex[w][h / 2]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h / 2; y++) { + c[x][y] = Complex.ofCartesian(i[x][y * 2], i[x][y * 2 + 1]); + } + } + } + return c; + } + + /** + * Converts a 2D interleaved complex {@code double[][]} array to a + * {@code Complex[][]} array. The second d level of the array is assumed + * to be interleaved. + * + * @param d 2D complex interleaved array + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] interleaved2Complex(double[][] d) { + return interleaved2Complex(d, 1); + } + + /** + * Converts a 3D interleaved complex {@code double[][][]} array to a + * {@code Complex[][][]} array. + * + * @param i 3D complex interleaved array + * @param interleavedDim Depth level of the array to interleave + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] interleaved2Complex(double[][][] i, int interleavedDim) { + if (interleavedDim > 2 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + final int w = i.length; + final int h = i[0].length; + final int d = i[0][0].length; + Complex[][][] c; + if (interleavedDim == 0) { + c = new Complex[w / 2][h][d]; + for (int x = 0; x < w / 2; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + c[x][y][z] = Complex.ofCartesian(i[x * 2][y][z], i[x * 2 + 1][y][z]); + } + } + } + } else if (interleavedDim == 1) { + c = new Complex[w][h / 2][d]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h / 2; y++) { + for (int z = 0; z < d; z++) { + c[x][y][z] = Complex.ofCartesian(i[x][y * 2][z], i[x][y * 2 + 1][z]); + } + } + } + } else { + c = new Complex[w][h][d / 2]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d / 2; z++) { + c[x][y][z] = Complex.ofCartesian(i[x][y][z * 2], i[x][y][z * 2 + 1]); + } + } + } + } + return c; + } + + /** + * Converts a 4D interleaved complex {@code double[][][][]} array to a + * {@code Complex[][][][]} array. + * + * @param i 4D complex interleaved array + * @param interleavedDim Depth level of the array to interleave + * @return 4D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][][] interleaved2Complex(double[][][][] i, int interleavedDim) { + if (interleavedDim > 2 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + final int w = i.length; + final int h = i[0].length; + final int d = i[0][0].length; + final int v = i[0][0][0].length; + Complex[][][][] c; + if (interleavedDim == 0) { + c = new Complex[w / 2][h][d][v]; + for (int x = 0; x < w / 2; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + for (int t = 0; t < v; t++) { + c[x][y][z][t] = Complex.ofCartesian(i[x * 2][y][z][t], i[x * 2 + 1][y][z][t]); + } + } + } + } + } else if (interleavedDim == 1) { + c = new Complex[w][h / 2][d][v]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h / 2; y++) { + for (int z = 0; z < d; z++) { + for (int t = 0; t < v; t++) { + c[x][y][z][t] = Complex.ofCartesian(i[x][y * 2][z][t], i[x][y * 2 + 1][z][t]); + } + } + } + } + } else if (interleavedDim == 2) { + c = new Complex[w][h][d / 2][v]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d / 2; z++) { + for (int t = 0; t < v; t++) { + c[x][y][z][t] = Complex.ofCartesian(i[x][y][z * 2][t], i[x][y][z * 2 + 1][t]); + } + } + } + } + } else { + c = new Complex[w][h][d][v / 2]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + for (int t = 0; t < v / 2; t++) { + c[x][y][z][t] = Complex.ofCartesian(i[x][y][z][t * 2], i[x][y][z][t * 2 + 1]); + } + } + } + } + } + return c; + } + + /** + * Converts a 3D interleaved complex {@code double[][][]} array to a + * {@code Complex[][][]} array. The third d level is assumed to be + * interleaved. + * + * @param d 3D complex interleaved array + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] interleaved2Complex(double[][][] d) { + return interleaved2Complex(d, 2); + } + + /** + * Converts a 2D interleaved complex {@code float[][]} array to a + * {@code Complex[][]} array. + * + * @param i 2D complex interleaved float array + * @param interleavedDim Depth level of the array to interleave + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] interleaved2Complex(float[][] i, int interleavedDim) { + if (interleavedDim > 1 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + final int w = i.length; + final int h = i[0].length; + Complex[][] c; + if (interleavedDim == 0) { + c = new Complex[w / 2][h]; + for (int x = 0; x < w / 2; x++) { + for (int y = 0; y < h; y++) { + c[x][y] = Complex.ofCartesian(i[x * 2][y], i[x * 2 + 1][y]); + } + } + } else { + c = new Complex[w][h / 2]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h / 2; y++) { + c[x][y] = Complex.ofCartesian(i[x][y * 2], i[x][y * 2 + 1]); + } + } + } + return c; + } + + /** + * Converts a 2D interleaved complex {@code float[][]} array to a + * {@code Complex[][]} array. The second d level of the array is assumed + * to be interleaved. + * + * @param d 2D complex interleaved float array + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] interleaved2Complex(float[][] d) { + return interleaved2Complex(d, 1); + } + + /** + * Converts a 3D interleaved complex {@code float[][][]} array to a + * {@code Complex[][][]} array. + * + * @param i 3D complex interleaved float array + * @param interleavedDim Depth level of the array to interleave + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] interleaved2Complex(float[][][] i, int interleavedDim) { + if (interleavedDim > 2 || interleavedDim < 0) { + throw new IndexOutOfRangeException(interleavedDim); + } + final int w = i.length; + final int h = i[0].length; + final int d = i[0][0].length; + Complex[][][] c; + if (interleavedDim == 0) { + c = new Complex[w / 2][h][d]; + for (int x = 0; x < w/2; x ++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d; z++) { + c[x][y][z] = Complex.ofCartesian(i[x * 2][y][z], i[x * 2 + 1][y][z]); + } + } + } + } else if (interleavedDim == 1) { + c = new Complex[w][h / 2][d]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h/2; y ++) { + for (int z = 0; z < d; z++) { + c[x][y][z] = Complex.ofCartesian(i[x][y * 2][z], i[x][y * 2 + 1][z]); + } + } + } + } else { + c = new Complex[w][h][d / 2]; + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + for (int z = 0; z < d/2; z++) { + c[x][y][z] = Complex.ofCartesian(i[x][y][z * 2], i[x][y][z * 2 + 1]); + } + } + } + } + return c; + } + + /** + * Converts a 3D interleaved complex {@code float[][][]} array to a + * {@code Complex[]} array. The third level of the array is assumed to + * be interleaved. + * + * @param d 3D complex interleaved float array + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] interleaved2Complex(float[][][] d) { + return interleaved2Complex(d, 2); + } + + // SPLIT METHODS + + /** + * Converts a split complex array {@code double[] r, double[] i} to a + * {@code Complex[]} array. + * + * @param real real component + * @param imag imaginary component + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex[] split2Complex(double[] real, double[] imag) { + final int length = real.length; + final Complex[] c = new Complex[length]; + for (int n = 0; n < length; n++) { + c[n] = Complex.ofCartesian(real[n], imag[n]); + } + return c; + } + + /** + * Converts a 2D split complex array {@code double[][] r, double[][] i} to a + * 2D {@code Complex[][]} array. + * + * @param real real component + * @param imag imaginary component + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] split2Complex(double[][] real, double[][] imag) { + final int length = real.length; + Complex[][] c = new Complex[length][]; + for (int x = 0; x < length; x++) { + c[x] = split2Complex(real[x], imag[x]); + } + return c; + } + + /** + * Converts a 3D split complex array {@code double[][][] r, double[][][] i} + * to a 3D {@code Complex[][][]} array. + * + * @param real real component + * @param imag imaginary component + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] split2Complex(double[][][] real, double[][][] imag) { + final int length = real.length; + Complex[][][] c = new Complex[length][][]; + for (int x = 0; x < length; x++) { + c[x] = split2Complex(real[x], imag[x]); + } + return c; + } + + /** + * Converts a 4D split complex array {@code double[][][][] r, double[][][][] i} + * to a 4D {@code Complex[][][][]} array. + * + * @param real real component + * @param imag imaginary component + * @return 4D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][][] split2Complex(double[][][][] real, double[][][][] imag) { + final int length = real.length; + Complex[][][][] c = new Complex[length][][][]; + for (int x = 0; x < length; x++) { + c[x] = split2Complex(real[x], imag[x]); + } + return c; + } + + /** + * Converts a split complex array {@code float[] r, float[] i} to a + * {@code Complex[]} array. + * + * @param real real component + * @param imag imaginary component + * @return {@code Complex} array + * + * @since 1.0 + */ + public static Complex[] split2Complex(float[] real, float[] imag) { + final int length = real.length; + final Complex[] c = new Complex[length]; + for (int n = 0; n < length; n++) { + c[n] = Complex.ofCartesian(real[n], imag[n]); + } + return c; + } + + /** + * Converts a 2D split complex array {@code float[][] r, float[][] i} to a + * 2D {@code Complex[][]} array. + * + * @param real real component + * @param imag imaginary component + * @return 2D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][] split2Complex(float[][] real, float[][] imag) { + final int length = real.length; + Complex[][] c = new Complex[length][]; + for (int x = 0; x < length; x++) { + c[x] = split2Complex(real[x], imag[x]); + } + return c; + } + + /** + * Converts a 3D split complex array {@code float[][][] r, float[][][] i} to + * a 3D {@code Complex[][][]} array. + * + * @param real real component + * @param imag imaginary component + * @return 3D {@code Complex} array + * + * @since 1.0 + */ + public static Complex[][][] split2Complex(float[][][] real, float[][][] imag) { + final int length = real.length; + Complex[][][] c = new Complex[length][][]; + for (int x = 0; x < length; x++) { + c[x] = split2Complex(real[x], imag[x]); + } + return c; + } + + // MISC + + /** + * Initializes a {@code Complex[]} array to zero, to avoid + * NullPointerExceptions. + * + * @param c Complex array + * @return c + * + * @since 1.0 + */ + public static Complex[] initialize(Complex[] c) { + final int length = c.length; + for (int x = 0; x < length; x++) { + c[x] = Complex.ZERO; + } + return c; + } + + /** + * Initializes a {@code Complex[][]} array to zero, to avoid + * NullPointerExceptions. + * + * @param c {@code Complex} array + * @return c + * + * @since 1.0 + */ + public static Complex[][] initialize(Complex[][] c) { + final int length = c.length; + for (int x = 0; x < length; x++) { + c[x] = initialize(c[x]); + } + return c; + } + + /** + * Initializes a {@code Complex[][][]} array to zero, to avoid + * NullPointerExceptions. + * + * @param c {@code Complex} array + * @return c + * + * @since 1.0 + */ + public static Complex[][][] initialize(Complex[][][] c) { + final int length = c.length; + for (int x = 0; x < length; x++) { + c[x] = initialize(c[x]); + } + return c; + } + + /** + * Returns {@code double[]} containing absolute values (magnitudes) of a + * {@code Complex[]} array. + * + * @param c {@code Complex} array + * @return {@code double[]} + * + * @since 1.0 + */ + public static double[] abs(Complex[] c) { + final int length = c.length; + final double[] i = new double[length]; + for (int x = 0; x < length; x++) { + i[x] = c[x].abs(); + } + return i; + } + + /** + * Returns {@code double[]} containing arguments (phase angles) of a + * {@code Complex[]} array. + * + * @param c {@code Complex} array + * @return {@code double[]} array + * + * @since 1.0 + */ + public static double[] arg(Complex[] c) { + final int length = c.length; + final double[] i = new double[length]; + for (int x = 0; x < length; x++) { + i[x] = c[x].getArgument(); + } + return i; + } + + /** + * Exception to be throw when a negative value is passed as the modulus. + */ + private static class NegativeModulusException extends IllegalArgumentException { + /** + * @param r Wrong modulus. + */ + NegativeModulusException(double r) { + super("Modulus is negative: " + r); + } + } + + /** + * Exception to be throw when an out-of-range index value is passed. + */ + private static class IndexOutOfRangeException extends IllegalArgumentException { + /** + * @param i Wrong index. + */ + IndexOutOfRangeException(int i) { + super("Out of range: " + i); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/40418955/commons-numbers-complex-streams/src/main/java/org/apache/commons/numbers/complex/streams/package-info.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex-streams/src/main/java/org/apache/commons/numbers/complex/streams/package-info.java b/commons-numbers-complex-streams/src/main/java/org/apache/commons/numbers/complex/streams/package-info.java new file mode 100644 index 0000000..0b7c1cd --- /dev/null +++ b/commons-numbers-complex-streams/src/main/java/org/apache/commons/numbers/complex/streams/package-info.java @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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 numbers collections. + */ +package org.apache.commons.numbers.complex.streams; http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/40418955/commons-numbers-complex-streams/src/site/resources/profile.jacoco ---------------------------------------------------------------------- diff --git a/commons-numbers-complex-streams/src/site/resources/profile.jacoco b/commons-numbers-complex-streams/src/site/resources/profile.jacoco new file mode 100644 index 0000000..a12755f --- /dev/null +++ b/commons-numbers-complex-streams/src/site/resources/profile.jacoco @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ----------------------------------------------------------------------------- +# +# Empty file used to automatically trigger JaCoCo profile from commons parent pom
