<commit_before>/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.math;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.opengamma.math.number.ComplexNumber;

/**
 * 
 */
public class ComplexMathUtilsTest {
  private static final double V = 0.123;
  private static final double W = 0.456;
  private static final double X = 7.89;
  private static final double Y = -12.34;
  private static final ComplexNumber X_C = new ComplexNumber(X, 0);
  private static final ComplexNumber Z1 = new ComplexNumber(V, W);
  private static final ComplexNumber Z2 = new ComplexNumber(X, Y);
  private static final double EPS = 1e-9;

  @Test
  public void testNull() {
    try {
      ComplexMathUtils.add(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.add(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.add(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.add(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.arg(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.conjugate(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.divide(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.divide(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.divide(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.divide(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.exp(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.inverse(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.log(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.mod(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.multiply(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.multiply(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.multiply(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.multiply(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.pow(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.pow(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.pow(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.pow(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.sqrt(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.subtract(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.subtract(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.subtract(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.subtract(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
  }

  @Test
  public void testAddSubtract() {
    assertComplexEquals(ComplexMathUtils.subtract(ComplexMathUtils.add(Z1, Z2), Z2), Z1);
    assertComplexEquals(ComplexMathUtils.subtract(ComplexMathUtils.add(Z1, X), X), Z1);
    assertComplexEquals(ComplexMathUtils.subtract(ComplexMathUtils.add(X, Z1), Z1), X_C);
  }

  @Test
  public void testArg() {
    assertEquals(Math.atan2(W, V), ComplexMathUtils.arg(Z1), EPS);
  }

  @Test
  public void testConjugate() {
    assertComplexEquals(ComplexMathUtils.conjugate(ComplexMathUtils.conjugate(Z1)), Z1);
  }

  @Test
  public void testDivideMultiply() {
    assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(Z1, Z2), Z2), Z1);
    assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(Z1, X), X), Z1);
    assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(X, Z1), Z1), X_C);
  }

  @Test
  public void testExpLn() {
    assertComplexEquals(ComplexMathUtils.log(ComplexMathUtils.exp(Z1)), Z1);
    //TODO test principal value
  }

  @Test
  public void testInverse() {
    assertComplexEquals(ComplexMathUtils.inverse(ComplexMathUtils.inverse(Z1)), Z1);
  }

  @Test
  public void testModulus() {
    assertEquals(Math.sqrt(V * V + W * W), ComplexMathUtils.mod(Z1), EPS);
  }

  @Test
  public void testPower() {
    assertComplexEquals(ComplexMathUtils.pow(Z1, 0), new ComplexNumber(1, 0));
    assertComplexEquals(ComplexMathUtils.pow(X, new ComplexNumber(0, 0)), new ComplexNumber(1, 0));
    assertComplexEquals(ComplexMathUtils.sqrt(ComplexMathUtils.pow(Z1, 2)), Z1);
    assertComplexEquals(ComplexMathUtils.sqrt(ComplexMathUtils.pow(Z2, 2)), Z2);
    assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(Z1, 1. / 3), 3), Z1);
    assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(X, ComplexMathUtils.inverse(Z2)), Z2), new ComplexNumber(X, 0));
    assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(Z1, ComplexMathUtils.inverse(Z2)), Z2), Z1);
  }

  private void assertComplexEquals(final ComplexNumber z1, final ComplexNumber z2) {
    assertEquals(z1.getReal(), z2.getReal(), EPS);
    assertEquals(z1.getImaginary(), z2.getImaginary(), EPS);
  }

  private void assertStackTraceElement(final StackTraceElement[] ste) {
    assertEquals(ste[0].getClassName(), "com.opengamma.util.ArgumentChecker");
  }
}
<commit_msg>Had forgotten to test a method.<commit_after>/**
 * Copyright (C) 2009 - 2010 by OpenGamma Inc.
 *
 * Please see distribution for license.
 */
package com.opengamma.math;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.opengamma.math.number.ComplexNumber;

/**
 * 
 */
public class ComplexMathUtilsTest {
  private static final double V = 0.123;
  private static final double W = 0.456;
  private static final double X = 7.89;
  private static final double Y = -12.34;
  private static final ComplexNumber X_C = new ComplexNumber(X, 0);
  private static final ComplexNumber Z1 = new ComplexNumber(V, W);
  private static final ComplexNumber Z2 = new ComplexNumber(X, Y);
  private static final double EPS = 1e-9;

  @Test
  public void testNull() {
    try {
      ComplexMathUtils.add(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.add(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.add(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.add(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.arg(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.conjugate(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.divide(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.divide(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.divide(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.divide(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.exp(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.inverse(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.log(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.mod(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.multiply(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.multiply(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.multiply(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.multiply(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.pow(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.pow(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.pow(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.pow(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.sqrt(null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.subtract(null, Z1);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.subtract(Z1, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.subtract(X, null);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
    try {
      ComplexMathUtils.subtract(null, X);
    } catch (final NullPointerException e) {
      assertStackTraceElement(e.getStackTrace());
    }
  }

  @Test
  public void testAddSubtract() {
    assertComplexEquals(ComplexMathUtils.subtract(ComplexMathUtils.add(Z1, Z2), Z2), Z1);
    assertComplexEquals(ComplexMathUtils.subtract(ComplexMathUtils.add(Z1, X), X), Z1);
    assertComplexEquals(ComplexMathUtils.subtract(ComplexMathUtils.add(X, Z1), Z1), X_C);
  }

  @Test
  public void testArg() {
    assertEquals(Math.atan2(W, V), ComplexMathUtils.arg(Z1), EPS);
  }

  @Test
  public void testConjugate() {
    assertComplexEquals(ComplexMathUtils.conjugate(ComplexMathUtils.conjugate(Z1)), Z1);
  }

  @Test
  public void testDivideMultiply() {
    assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(Z1, Z2), Z2), Z1);
    assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(Z1, X), X), Z1);
    assertComplexEquals(ComplexMathUtils.multiply(ComplexMathUtils.divide(X, Z1), Z1), X_C);
    assertComplexEquals(ComplexMathUtils.multiply(X, Z1), ComplexMathUtils.multiply(Z1, X));
  }

  @Test
  public void testExpLn() {
    assertComplexEquals(ComplexMathUtils.log(ComplexMathUtils.exp(Z1)), Z1);
    //TODO test principal value
  }

  @Test
  public void testInverse() {
    assertComplexEquals(ComplexMathUtils.inverse(ComplexMathUtils.inverse(Z1)), Z1);
  }

  @Test
  public void testModulus() {
    assertEquals(Math.sqrt(V * V + W * W), ComplexMathUtils.mod(Z1), EPS);
  }

  @Test
  public void testPower() {
    assertComplexEquals(ComplexMathUtils.pow(Z1, 0), new ComplexNumber(1, 0));
    assertComplexEquals(ComplexMathUtils.pow(X, new ComplexNumber(0, 0)), new ComplexNumber(1, 0));
    assertComplexEquals(ComplexMathUtils.sqrt(ComplexMathUtils.pow(Z1, 2)), Z1);
    assertComplexEquals(ComplexMathUtils.sqrt(ComplexMathUtils.pow(Z2, 2)), Z2);
    assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(Z1, 1. / 3), 3), Z1);
    assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(X, ComplexMathUtils.inverse(Z2)), Z2), new ComplexNumber(X, 0));
    assertComplexEquals(ComplexMathUtils.pow(ComplexMathUtils.pow(Z1, ComplexMathUtils.inverse(Z2)), Z2), Z1);
  }

  private void assertComplexEquals(final ComplexNumber z1, final ComplexNumber z2) {
    assertEquals(z1.getReal(), z2.getReal(), EPS);
    assertEquals(z1.getImaginary(), z2.getImaginary(), EPS);
  }

  private void assertStackTraceElement(final StackTraceElement[] ste) {
    assertEquals(ste[0].getClassName(), "com.opengamma.util.ArgumentChecker");
  }
}

{'fraction_non_alphanumeric': 0.11692627206645899, 'fraction_numerical': 0.013430252682589131, 'mean_word_length': 3.6765943671090966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4736756', 'n_tokens_mistral': 4445, 'n_tokens_neox': 4321, 'n_words': 853}