You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/09/03 21:11:15 UTC

[GitHub] [commons-numbers] sumanth-rajkumar opened a new pull request, #122: NUMBERS-186: Lists of Complex Numbers

sumanth-rajkumar opened a new pull request, #122:
URL: https://github.com/apache/commons-numbers/pull/122

   This PR introduces support for complex numbers in the complex package to allow operations to be performed on lists of complex numbers. The list can be efficiently iterated to apply all the operations in-place supported by the Complex class.
   
   Summary of changes
   
   ComplexListFunctionsTest - unit tests for complex operations on ComplexList


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [commons-numbers] aherbert commented on pull request #122: NUMBERS-186: Lists of Complex Numbers

Posted by GitBox <gi...@apache.org>.
aherbert commented on PR #122:
URL: https://github.com/apache/commons-numbers/pull/122#issuecomment-1237269408

   Closed by 603a17f54e730e518d0bd0f2d9cef553a5e72808
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [commons-numbers] codecov-commenter commented on pull request #122: NUMBERS-186: Lists of Complex Numbers

Posted by GitBox <gi...@apache.org>.
codecov-commenter commented on PR #122:
URL: https://github.com/apache/commons-numbers/pull/122#issuecomment-1236199322

   # [Codecov](https://codecov.io/gh/apache/commons-numbers/pull/122?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#122](https://codecov.io/gh/apache/commons-numbers/pull/122?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (a0c09ff) into [complex-gsoc-2022](https://codecov.io/gh/apache/commons-numbers/commit/2bd5755565470ab94a9ec2460ea2989e92021aca?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (2bd5755) will **not change** coverage.
   > The diff coverage is `n/a`.
   
   ```diff
   @@                 Coverage Diff                  @@
   ##             complex-gsoc-2022     #122   +/-   ##
   ====================================================
     Coverage                99.13%   99.13%           
     Complexity                1699     1699           
   ====================================================
     Files                       65       65           
     Lines                     4274     4274           
     Branches                   836      836           
   ====================================================
     Hits                      4237     4237           
     Misses                      10       10           
     Partials                    27       27           
   ```
   
   
   
   :mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [commons-numbers] aherbert commented on a diff in pull request #122: NUMBERS-186: Lists of Complex Numbers

Posted by GitBox <gi...@apache.org>.
aherbert commented on code in PR #122:
URL: https://github.com/apache/commons-numbers/pull/122#discussion_r962331149


##########
commons-numbers-complex-arrays/src/main/java/org/apache/commons/numbers/complex/arrays/ComplexList.java:
##########
@@ -322,4 +325,79 @@ private String outOfBoundsMsg(int index) {
         return INDEX_MSG + index + SIZE_MSG + size;
     }
 
+    /**
+     * This class acts as an iterator over the ComplexList class.
+     */
+    private class ComplexCursor implements ComplexSink<Void> {
+
+        /**
+         * Index of element to be returned by subsequent call to next.
+         */
+        private int index;
+
+        /**
+         * The modCount value that the cursor believes that the backing List should have.
+         * If this expectation is violated, the cursor has detected concurrent modification.
+         */
+        private final int expectedModCount = modCount;
+
+        /**
+         * Checks if the iteration has more elements.
+         * @return true if the iteration has more elements.
+         */
+        public boolean next() {
+            return index != size;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public Void apply(double real, double imaginary) {
+            ComplexList.this.realAndImagParts[index * 2] = real;
+            ComplexList.this.realAndImagParts[(index * 2) + 1] = imaginary;
+            return null;
+        }
+
+        /**
+         * Gets the real part \( a \) of the complex number \( (a + i b) \) in the list.
+         *
+         * @return The real part.
+         */
+        public double real() {
+            return ComplexList.this.realAndImagParts[index * 2];
+        }
+
+        /**
+         * Gets the imaginary part \( b \) of the complex number \( (a + i b) \) in the list.
+         *
+         * @return The imaginary part.
+         */
+        public double imag() {
+            return ComplexList.this.realAndImagParts[(index * 2) + 1];
+        }
+
+        /**
+         * Checks for concurrent modification by looking at the modCount.
+         * @throws ConcurrentModificationException if expected modCount isn't equal to modCount.
+         */
+        final void checkForComodification() {
+            if (modCount != expectedModCount) {
+                throw new ConcurrentModificationException();
+            }
+        }
+    }
+
+    /**
+     * Replaces each element of the list with the result of applying the operator to that element.
+     * @param operator The operator to apply to each element.
+     */
+    public void replaceAll(ComplexUnaryOperator<Void> operator) {
+        ComplexCursor cursor = new ComplexCursor();
+        while (cursor.next()) {

Review Comment:
   This still advances the index outside of the cursor. I think the cursor should have that responsibility.
   
   The cursor could be changed to use the action in a tryAdvance(action) pattern. Alternatively if the cursor will not be reused then it could be removed and the logic placed directly here. It would eliminate the object allocation and simplify the loop logic.
   
   ```Java
   final double[] parts = ...;
   final int m = size;
   for (int i = 0; i < m; i++) {
       final int index = i << 1;
       operator.apply(parts[index], parts[index + 1], (x, y) -> { parts[index] = x; parts[index + 1] = y; });
   }
   // check for comodification
   ```
   Doing it this way uses a local copy of the list and size for efficiency.



##########
commons-numbers-complex-arrays/src/test/java/org/apache/commons/numbers/complex/arrays/ComplexListFunctionsTest.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF 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.arrays;
+
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.complex.ComplexFunctions;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
+
+public class ComplexListFunctionsTest {

Review Comment:
   These tests can be moved to ComplexListTest, or at least the test class should be commented to state what tests are in this that are not in ComplexListTest.



##########
commons-numbers-complex-arrays/src/test/java/org/apache/commons/numbers/complex/arrays/ComplexListFunctionsTest.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF 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.arrays;
+
+import org.apache.commons.numbers.complex.Complex;
+import org.apache.commons.numbers.complex.ComplexFunctions;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
+
+public class ComplexListFunctionsTest {
+
+    @Test
+    void testComplexUnaryOperator() {
+        List<Complex> objectList = generateList(10);
+        ComplexList actualList = new ComplexList();
+        actualList.addAll(objectList);
+        objectList.replaceAll(Complex::conj);
+        actualList.replaceAll(ComplexFunctions::conj);
+        Assertions.assertEquals(objectList, actualList);
+    }
+
+    @Test
+    void testComplexBinaryOperator() {
+        List<Complex> objectList = generateList(10);
+        double r = 2;
+        double i = 3;
+        Complex multiplier = Complex.ofCartesian(r, i);
+        ComplexList actualList = new ComplexList();
+        actualList.addAll(objectList);
+        objectList.replaceAll(c -> c.multiply(multiplier));
+        actualList.replaceAll((x, y, action) -> ComplexFunctions.multiply(x, y, r, i, action));
+        Assertions.assertEquals(objectList, actualList);
+    }
+
+    @Test
+    void testComplexScalarFunction() {
+        List<Complex> objectList = generateList(10);
+        double factor = 2;
+        ComplexList actualList = new ComplexList();
+        actualList.addAll(objectList);
+        objectList.replaceAll(c -> c.pow(factor));
+        actualList.replaceAll((x, y, action) -> ComplexFunctions.pow(x, y, factor, action));
+        Assertions.assertEquals(objectList, actualList);
+    }
+
+    private List<Complex> generateList(int size) {

Review Comment:
   Can be static. Should have a comment indicating what the function generates



##########
commons-numbers-complex-arrays/src/test/java/org/apache/commons/numbers/complex/arrays/ComplexListTest.java:
##########
@@ -19,16 +19,15 @@
 
 import org.apache.commons.numbers.complex.Complex;
 import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
+import org.junit.jupiter.api.Test;

Review Comment:
   Reorganising imports should not be done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [commons-numbers] aherbert closed pull request #122: NUMBERS-186: Lists of Complex Numbers

Posted by GitBox <gi...@apache.org>.
aherbert closed pull request #122: NUMBERS-186: Lists of Complex Numbers
URL: https://github.com/apache/commons-numbers/pull/122


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org