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/04 15:29:19 UTC

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

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