Return-Path: X-Original-To: apmail-commons-notifications-archive@minotaur.apache.org Delivered-To: apmail-commons-notifications-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 83A5710417 for ; Sun, 17 May 2015 17:06:03 +0000 (UTC) Received: (qmail 77877 invoked by uid 500); 17 May 2015 17:06:03 -0000 Delivered-To: apmail-commons-notifications-archive@commons.apache.org Received: (qmail 77857 invoked by uid 500); 17 May 2015 17:06:03 -0000 Mailing-List: contact notifications-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list notifications@commons.apache.org Received: (qmail 77768 invoked by uid 99); 17 May 2015 17:06:03 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 17 May 2015 17:06:03 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id 4CFE6AC0A56 for ; Sun, 17 May 2015 17:06:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r951633 [25/49] - in /websites/production/commons/content/proper/commons-math: xref-test/ xref-test/org/apache/commons/math3/ xref-test/org/apache/commons/math3/analysis/ xref-test/org/apache/commons/math3/analysis/differentiation/ xref-tes... Date: Sun, 17 May 2015 17:05:54 -0000 To: notifications@commons.apache.org From: luc@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20150517170603.4CFE6AC0A56@hades.apache.org> Modified: websites/production/commons/content/proper/commons-math/xref-test/org/apache/commons/math3/util/MathArraysTest.html ============================================================================== --- websites/production/commons/content/proper/commons-math/xref-test/org/apache/commons/math3/util/MathArraysTest.html (original) +++ websites/production/commons/content/proper/commons-math/xref-test/org/apache/commons/math3/util/MathArraysTest.html Sun May 17 17:05:50 2015 @@ -717,446 +717,479 @@ 709 } 710 711 @Test -712 public void testLinearCombinationInfinite() { -713 final double[][] a = new double[][] { -714 { 1, 2, 3, 4}, -715 { 1, Double.POSITIVE_INFINITY, 3, 4}, -716 { 1, 2, Double.POSITIVE_INFINITY, 4}, -717 { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY}, -718 { 1, 2, 3, 4}, -719 { 1, 2, 3, 4}, -720 { 1, 2, 3, 4}, -721 { 1, 2, 3, 4} -722 }; -723 final double[][] b = new double[][] { -724 { 1, -2, 3, 4}, -725 { 1, -2, 3, 4}, -726 { 1, -2, 3, 4}, -727 { 1, -2, 3, 4}, -728 { 1, Double.POSITIVE_INFINITY, 3, 4}, -729 { 1, -2, Double.POSITIVE_INFINITY, 4}, -730 { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY}, -731 { Double.NaN, -2, 3, 4} -732 }; -733 -734 Assert.assertEquals(-3, -735 MathArrays.linearCombination(a[0][0], b[0][0], -736 a[0][1], b[0][1]), -737 1.0e-10); -738 Assert.assertEquals(6, -739 MathArrays.linearCombination(a[0][0], b[0][0], -740 a[0][1], b[0][1], -741 a[0][2], b[0][2]), -742 1.0e-10); -743 Assert.assertEquals(22, -744 MathArrays.linearCombination(a[0][0], b[0][0], -745 a[0][1], b[0][1], -746 a[0][2], b[0][2], -747 a[0][3], b[0][3]), -748 1.0e-10); -749 Assert.assertEquals(22, MathArrays.linearCombination(a[0], b[0]), 1.0e-10); -750 -751 Assert.assertEquals(Double.NEGATIVE_INFINITY, -752 MathArrays.linearCombination(a[1][0], b[1][0], -753 a[1][1], b[1][1]), -754 1.0e-10); -755 Assert.assertEquals(Double.NEGATIVE_INFINITY, -756 MathArrays.linearCombination(a[1][0], b[1][0], -757 a[1][1], b[1][1], -758 a[1][2], b[1][2]), -759 1.0e-10); -760 Assert.assertEquals(Double.NEGATIVE_INFINITY, -761 MathArrays.linearCombination(a[1][0], b[1][0], -762 a[1][1], b[1][1], -763 a[1][2], b[1][2], -764 a[1][3], b[1][3]), -765 1.0e-10); -766 Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[1], b[1]), 1.0e-10); -767 -768 Assert.assertEquals(-3, -769 MathArrays.linearCombination(a[2][0], b[2][0], -770 a[2][1], b[2][1]), -771 1.0e-10); -772 Assert.assertEquals(Double.POSITIVE_INFINITY, -773 MathArrays.linearCombination(a[2][0], b[2][0], -774 a[2][1], b[2][1], -775 a[2][2], b[2][2]), -776 1.0e-10); -777 Assert.assertEquals(Double.POSITIVE_INFINITY, -778 MathArrays.linearCombination(a[2][0], b[2][0], -779 a[2][1], b[2][1], -780 a[2][2], b[2][2], -781 a[2][3], b[2][3]), -782 1.0e-10); -783 Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[2], b[2]), 1.0e-10); -784 -785 Assert.assertEquals(Double.NEGATIVE_INFINITY, -786 MathArrays.linearCombination(a[3][0], b[3][0], -787 a[3][1], b[3][1]), -788 1.0e-10); -789 Assert.assertEquals(Double.NEGATIVE_INFINITY, -790 MathArrays.linearCombination(a[3][0], b[3][0], -791 a[3][1], b[3][1], -792 a[3][2], b[3][2]), -793 1.0e-10); -794 Assert.assertEquals(Double.NEGATIVE_INFINITY, -795 MathArrays.linearCombination(a[3][0], b[3][0], -796 a[3][1], b[3][1], -797 a[3][2], b[3][2], -798 a[3][3], b[3][3]), -799 1.0e-10); -800 Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[3], b[3]), 1.0e-10); -801 -802 Assert.assertEquals(Double.POSITIVE_INFINITY, -803 MathArrays.linearCombination(a[4][0], b[4][0], -804 a[4][1], b[4][1]), -805 1.0e-10); -806 Assert.assertEquals(Double.POSITIVE_INFINITY, -807 MathArrays.linearCombination(a[4][0], b[4][0], -808 a[4][1], b[4][1], -809 a[4][2], b[4][2]), -810 1.0e-10); -811 Assert.assertEquals(Double.POSITIVE_INFINITY, -812 MathArrays.linearCombination(a[4][0], b[4][0], -813 a[4][1], b[4][1], -814 a[4][2], b[4][2], -815 a[4][3], b[4][3]), -816 1.0e-10); -817 Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[4], b[4]), 1.0e-10); -818 -819 Assert.assertEquals(-3, -820 MathArrays.linearCombination(a[5][0], b[5][0], -821 a[5][1], b[5][1]), -822 1.0e-10); -823 Assert.assertEquals(Double.POSITIVE_INFINITY, -824 MathArrays.linearCombination(a[5][0], b[5][0], -825 a[5][1], b[5][1], -826 a[5][2], b[5][2]), -827 1.0e-10); -828 Assert.assertEquals(Double.POSITIVE_INFINITY, -829 MathArrays.linearCombination(a[5][0], b[5][0], -830 a[5][1], b[5][1], -831 a[5][2], b[5][2], -832 a[5][3], b[5][3]), -833 1.0e-10); -834 Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[5], b[5]), 1.0e-10); -835 -836 Assert.assertEquals(Double.POSITIVE_INFINITY, -837 MathArrays.linearCombination(a[6][0], b[6][0], -838 a[6][1], b[6][1]), -839 1.0e-10); -840 Assert.assertEquals(Double.POSITIVE_INFINITY, -841 MathArrays.linearCombination(a[6][0], b[6][0], -842 a[6][1], b[6][1], -843 a[6][2], b[6][2]), -844 1.0e-10); -845 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[6][0], b[6][0], -846 a[6][1], b[6][1], -847 a[6][2], b[6][2], -848 a[6][3], b[6][3]))); -849 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[6], b[6]))); -850 -851 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], -852 a[7][1], b[7][1]))); -853 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], -854 a[7][1], b[7][1], -855 a[7][2], b[7][2]))); -856 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], -857 a[7][1], b[7][1], -858 a[7][2], b[7][2], -859 a[7][3], b[7][3]))); -860 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7], b[7]))); -861 } -862 -863 @Test -864 public void testArrayEquals() { -865 Assert.assertFalse(MathArrays.equals(new double[] { 1d }, null)); -866 Assert.assertFalse(MathArrays.equals(null, new double[] { 1d })); -867 Assert.assertTrue(MathArrays.equals((double[]) null, (double[]) null)); +712 public void testLinearCombinationHuge() { +713 int scale = 971; +714 final double[] a = new double[] { +715 -1321008684645961.0 / 268435456.0, +716 -5774608829631843.0 / 268435456.0, +717 -7645843051051357.0 / 8589934592.0 +718 }; +719 final double[] b = new double[] { +720 -5712344449280879.0 / 2097152.0, +721 -4550117129121957.0 / 2097152.0, +722 8846951984510141.0 / 131072.0 +723 }; +724 +725 double[] scaledA = new double[a.length]; +726 double[] scaledB = new double[b.length]; +727 for (int i = 0; i < scaledA.length; ++i) { +728 scaledA[i] = FastMath.scalb(a[i], -scale); +729 scaledB[i] = FastMath.scalb(b[i], +scale); +730 } +731 final double abSumInline = MathArrays.linearCombination(scaledA[0], scaledB[0], +732 scaledA[1], scaledB[1], +733 scaledA[2], scaledB[2]); +734 final double abSumArray = MathArrays.linearCombination(scaledA, scaledB); +735 +736 Assert.assertEquals(abSumInline, abSumArray, 0); +737 Assert.assertEquals(-1.8551294182586248737720779899, abSumInline, 1.0e-15); +738 +739 final double naive = scaledA[0] * scaledB[0] + scaledA[1] * scaledB[1] + scaledA[2] * scaledB[2]; +740 Assert.assertTrue(FastMath.abs(naive - abSumInline) > 1.5); +741 +742 } +743 +744 @Test +745 public void testLinearCombinationInfinite() { +746 final double[][] a = new double[][] { +747 { 1, 2, 3, 4}, +748 { 1, Double.POSITIVE_INFINITY, 3, 4}, +749 { 1, 2, Double.POSITIVE_INFINITY, 4}, +750 { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY}, +751 { 1, 2, 3, 4}, +752 { 1, 2, 3, 4}, +753 { 1, 2, 3, 4}, +754 { 1, 2, 3, 4} +755 }; +756 final double[][] b = new double[][] { +757 { 1, -2, 3, 4}, +758 { 1, -2, 3, 4}, +759 { 1, -2, 3, 4}, +760 { 1, -2, 3, 4}, +761 { 1, Double.POSITIVE_INFINITY, 3, 4}, +762 { 1, -2, Double.POSITIVE_INFINITY, 4}, +763 { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY}, +764 { Double.NaN, -2, 3, 4} +765 }; +766 +767 Assert.assertEquals(-3, +768 MathArrays.linearCombination(a[0][0], b[0][0], +769 a[0][1], b[0][1]), +770 1.0e-10); +771 Assert.assertEquals(6, +772 MathArrays.linearCombination(a[0][0], b[0][0], +773 a[0][1], b[0][1], +774 a[0][2], b[0][2]), +775 1.0e-10); +776 Assert.assertEquals(22, +777 MathArrays.linearCombination(a[0][0], b[0][0], +778 a[0][1], b[0][1], +779 a[0][2], b[0][2], +780 a[0][3], b[0][3]), +781 1.0e-10); +782 Assert.assertEquals(22, MathArrays.linearCombination(a[0], b[0]), 1.0e-10); +783 +784 Assert.assertEquals(Double.NEGATIVE_INFINITY, +785 MathArrays.linearCombination(a[1][0], b[1][0], +786 a[1][1], b[1][1]), +787 1.0e-10); +788 Assert.assertEquals(Double.NEGATIVE_INFINITY, +789 MathArrays.linearCombination(a[1][0], b[1][0], +790 a[1][1], b[1][1], +791 a[1][2], b[1][2]), +792 1.0e-10); +793 Assert.assertEquals(Double.NEGATIVE_INFINITY, +794 MathArrays.linearCombination(a[1][0], b[1][0], +795 a[1][1], b[1][1], +796 a[1][2], b[1][2], +797 a[1][3], b[1][3]), +798 1.0e-10); +799 Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[1], b[1]), 1.0e-10); +800 +801 Assert.assertEquals(-3, +802 MathArrays.linearCombination(a[2][0], b[2][0], +803 a[2][1], b[2][1]), +804 1.0e-10); +805 Assert.assertEquals(Double.POSITIVE_INFINITY, +806 MathArrays.linearCombination(a[2][0], b[2][0], +807 a[2][1], b[2][1], +808 a[2][2], b[2][2]), +809 1.0e-10); +810 Assert.assertEquals(Double.POSITIVE_INFINITY, +811 MathArrays.linearCombination(a[2][0], b[2][0], +812 a[2][1], b[2][1], +813 a[2][2], b[2][2], +814 a[2][3], b[2][3]), +815 1.0e-10); +816 Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[2], b[2]), 1.0e-10); +817 +818 Assert.assertEquals(Double.NEGATIVE_INFINITY, +819 MathArrays.linearCombination(a[3][0], b[3][0], +820 a[3][1], b[3][1]), +821 1.0e-10); +822 Assert.assertEquals(Double.NEGATIVE_INFINITY, +823 MathArrays.linearCombination(a[3][0], b[3][0], +824 a[3][1], b[3][1], +825 a[3][2], b[3][2]), +826 1.0e-10); +827 Assert.assertEquals(Double.NEGATIVE_INFINITY, +828 MathArrays.linearCombination(a[3][0], b[3][0], +829 a[3][1], b[3][1], +830 a[3][2], b[3][2], +831 a[3][3], b[3][3]), +832 1.0e-10); +833 Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[3], b[3]), 1.0e-10); +834 +835 Assert.assertEquals(Double.POSITIVE_INFINITY, +836 MathArrays.linearCombination(a[4][0], b[4][0], +837 a[4][1], b[4][1]), +838 1.0e-10); +839 Assert.assertEquals(Double.POSITIVE_INFINITY, +840 MathArrays.linearCombination(a[4][0], b[4][0], +841 a[4][1], b[4][1], +842 a[4][2], b[4][2]), +843 1.0e-10); +844 Assert.assertEquals(Double.POSITIVE_INFINITY, +845 MathArrays.linearCombination(a[4][0], b[4][0], +846 a[4][1], b[4][1], +847 a[4][2], b[4][2], +848 a[4][3], b[4][3]), +849 1.0e-10); +850 Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[4], b[4]), 1.0e-10); +851 +852 Assert.assertEquals(-3, +853 MathArrays.linearCombination(a[5][0], b[5][0], +854 a[5][1], b[5][1]), +855 1.0e-10); +856 Assert.assertEquals(Double.POSITIVE_INFINITY, +857 MathArrays.linearCombination(a[5][0], b[5][0], +858 a[5][1], b[5][1], +859 a[5][2], b[5][2]), +860 1.0e-10); +861 Assert.assertEquals(Double.POSITIVE_INFINITY, +862 MathArrays.linearCombination(a[5][0], b[5][0], +863 a[5][1], b[5][1], +864 a[5][2], b[5][2], +865 a[5][3], b[5][3]), +866 1.0e-10); +867 Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[5], b[5]), 1.0e-10); 868 -869 Assert.assertFalse(MathArrays.equals(new double[] { 1d }, new double[0])); -870 Assert.assertTrue(MathArrays.equals(new double[] { 1d }, new double[] { 1d })); -871 Assert.assertTrue(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY, -872 Double.NEGATIVE_INFINITY, 1d, 0d }, -873 new double[] { Double.POSITIVE_INFINITY, -874 Double.NEGATIVE_INFINITY, 1d, 0d })); -875 Assert.assertFalse(MathArrays.equals(new double[] { Double.NaN }, -876 new double[] { Double.NaN })); -877 Assert.assertFalse(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY }, -878 new double[] { Double.NEGATIVE_INFINITY })); -879 Assert.assertFalse(MathArrays.equals(new double[] { 1d }, -880 new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) })); -881 -882 } +869 Assert.assertEquals(Double.POSITIVE_INFINITY, +870 MathArrays.linearCombination(a[6][0], b[6][0], +871 a[6][1], b[6][1]), +872 1.0e-10); +873 Assert.assertEquals(Double.POSITIVE_INFINITY, +874 MathArrays.linearCombination(a[6][0], b[6][0], +875 a[6][1], b[6][1], +876 a[6][2], b[6][2]), +877 1.0e-10); +878 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[6][0], b[6][0], +879 a[6][1], b[6][1], +880 a[6][2], b[6][2], +881 a[6][3], b[6][3]))); +882 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[6], b[6]))); 883 -884 @Test -885 public void testArrayEqualsIncludingNaN() { -886 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, null)); -887 Assert.assertFalse(MathArrays.equalsIncludingNaN(null, new double[] { 1d })); -888 Assert.assertTrue(MathArrays.equalsIncludingNaN((double[]) null, (double[]) null)); -889 -890 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[0])); -891 Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[] { 1d })); -892 Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] { Double.NaN, Double.POSITIVE_INFINITY, -893 Double.NEGATIVE_INFINITY, 1d, 0d }, -894 new double[] { Double.NaN, Double.POSITIVE_INFINITY, -895 Double.NEGATIVE_INFINITY, 1d, 0d })); -896 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { Double.POSITIVE_INFINITY }, -897 new double[] { Double.NEGATIVE_INFINITY })); -898 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, -899 new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) })); -900 } +884 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], +885 a[7][1], b[7][1]))); +886 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], +887 a[7][1], b[7][1], +888 a[7][2], b[7][2]))); +889 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], +890 a[7][1], b[7][1], +891 a[7][2], b[7][2], +892 a[7][3], b[7][3]))); +893 Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7], b[7]))); +894 } +895 +896 @Test +897 public void testArrayEquals() { +898 Assert.assertFalse(MathArrays.equals(new double[] { 1d }, null)); +899 Assert.assertFalse(MathArrays.equals(null, new double[] { 1d })); +900 Assert.assertTrue(MathArrays.equals((double[]) null, (double[]) null)); 901 -902 @Test -903 public void testNormalizeArray() { -904 double[] testValues1 = new double[] {1, 1, 2}; -905 TestUtils.assertEquals( new double[] {.25, .25, .5}, -906 MathArrays.normalizeArray(testValues1, 1), -907 Double.MIN_VALUE); -908 -909 double[] testValues2 = new double[] {-1, -1, 1}; -910 TestUtils.assertEquals( new double[] {1, 1, -1}, -911 MathArrays.normalizeArray(testValues2, 1), -912 Double.MIN_VALUE); -913 -914 // Ignore NaNs -915 double[] testValues3 = new double[] {-1, -1, Double.NaN, 1, Double.NaN}; -916 TestUtils.assertEquals( new double[] {1, 1,Double.NaN, -1, Double.NaN}, -917 MathArrays.normalizeArray(testValues3, 1), -918 Double.MIN_VALUE); -919 -920 // Zero sum -> MathArithmeticException -921 double[] zeroSum = new double[] {-1, 1}; -922 try { -923 MathArrays.normalizeArray(zeroSum, 1); -924 Assert.fail("expecting MathArithmeticException"); -925 } catch (MathArithmeticException ex) {} -926 -927 // Infinite elements -> MathArithmeticException -928 double[] hasInf = new double[] {1, 2, 1, Double.NEGATIVE_INFINITY}; -929 try { -930 MathArrays.normalizeArray(hasInf, 1); -931 Assert.fail("expecting MathIllegalArgumentException"); -932 } catch (MathIllegalArgumentException ex) {} -933 -934 // Infinite target -> MathIllegalArgumentException -935 try { -936 MathArrays.normalizeArray(testValues1, Double.POSITIVE_INFINITY); -937 Assert.fail("expecting MathIllegalArgumentException"); -938 } catch (MathIllegalArgumentException ex) {} -939 -940 // NaN target -> MathIllegalArgumentException -941 try { -942 MathArrays.normalizeArray(testValues1, Double.NaN); -943 Assert.fail("expecting MathIllegalArgumentException"); -944 } catch (MathIllegalArgumentException ex) {} -945 } -946 -947 @Test -948 public void testConvolve() { -949 /* Test Case (obtained via SciPy) -950 * x=[1.2,-1.8,1.4] -951 * h=[1,0.8,0.5,0.3] -952 * convolve(x,h) -> array([ 1.2 , -0.84, 0.56, 0.58, 0.16, 0.42]) -953 */ -954 double[] x1 = { 1.2, -1.8, 1.4 }; -955 double[] h1 = { 1, 0.8, 0.5, 0.3 }; -956 double[] y1 = { 1.2, -0.84, 0.56, 0.58, 0.16, 0.42 }; -957 double tolerance = 1e-13; -958 -959 double[] yActual = MathArrays.convolve(x1, h1); -960 Assert.assertArrayEquals(y1, yActual, tolerance); -961 -962 double[] x2 = { 1, 2, 3 }; -963 double[] h2 = { 0, 1, 0.5 }; -964 double[] y2 = { 0, 1, 2.5, 4, 1.5 }; -965 -966 yActual = MathArrays.convolve(x2, h2); -967 Assert.assertArrayEquals(y2, yActual, tolerance); -968 -969 try { -970 MathArrays.convolve(new double[]{1, 2}, null); -971 Assert.fail("an exception should have been thrown"); -972 } catch (NullArgumentException e) { -973 // expected behavior -974 } -975 -976 try { -977 MathArrays.convolve(null, new double[]{1, 2}); -978 Assert.fail("an exception should have been thrown"); -979 } catch (NullArgumentException e) { -980 // expected behavior -981 } -982 -983 try { -984 MathArrays.convolve(new double[]{1, 2}, new double[]{}); -985 Assert.fail("an exception should have been thrown"); -986 } catch (NoDataException e) { -987 // expected behavior -988 } -989 -990 try { -991 MathArrays.convolve(new double[]{}, new double[]{1, 2}); -992 Assert.fail("an exception should have been thrown"); -993 } catch (NoDataException e) { -994 // expected behavior -995 } -996 -997 try { -998 MathArrays.convolve(new double[]{}, new double[]{}); -999 Assert.fail("an exception should have been thrown"); -1000 } catch (NoDataException e) { -1001 // expected behavior -1002 } -1003 } -1004 -1005 @Test -1006 public void testShuffleTail() { -1007 final int[] orig = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; -1008 final int[] list = orig.clone(); -1009 final int start = 4; -1010 MathArrays.shuffle(list, start, MathArrays.Position.TAIL, new Well1024a(7654321L)); -1011 -1012 // Ensure that all entries below index "start" did not move. -1013 for (int i = 0; i < start; i++) { -1014 Assert.assertEquals(orig[i], list[i]); -1015 } -1016 -1017 // Ensure that at least one entry has moved. -1018 boolean ok = false; -1019 for (int i = start; i < orig.length - 1; i++) { -1020 if (orig[i] != list[i]) { -1021 ok = true; -1022 break; -1023 } -1024 } -1025 Assert.assertTrue(ok); -1026 } -1027 -1028 @Test -1029 public void testShuffleHead() { -1030 final int[] orig = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; -1031 final int[] list = orig.clone(); -1032 final int start = 4; -1033 MathArrays.shuffle(list, start, MathArrays.Position.HEAD, new Well1024a(1234567L)); -1034 -1035 // Ensure that all entries above index "start" did not move. -1036 for (int i = start + 1; i < orig.length; i++) { -1037 Assert.assertEquals(orig[i], list[i]); -1038 } -1039 -1040 // Ensure that at least one entry has moved. -1041 boolean ok = false; -1042 for (int i = 0; i <= start; i++) { -1043 if (orig[i] != list[i]) { -1044 ok = true; -1045 break; -1046 } -1047 } -1048 Assert.assertTrue(ok); -1049 } -1050 -1051 @Test -1052 public void testNatural() { -1053 final int n = 4; -1054 final int[] expected = {0, 1, 2, 3}; -1055 -1056 final int[] natural = MathArrays.natural(n); -1057 for (int i = 0; i < n; i++) { -1058 Assert.assertEquals(expected[i], natural[i]); -1059 } -1060 } -1061 -1062 @Test -1063 public void testNaturalZero() { -1064 final int[] natural = MathArrays.natural(0); -1065 Assert.assertEquals(0, natural.length); -1066 } +902 Assert.assertFalse(MathArrays.equals(new double[] { 1d }, new double[0])); +903 Assert.assertTrue(MathArrays.equals(new double[] { 1d }, new double[] { 1d })); +904 Assert.assertTrue(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY, +905 Double.NEGATIVE_INFINITY, 1d, 0d }, +906 new double[] { Double.POSITIVE_INFINITY, +907 Double.NEGATIVE_INFINITY, 1d, 0d })); +908 Assert.assertFalse(MathArrays.equals(new double[] { Double.NaN }, +909 new double[] { Double.NaN })); +910 Assert.assertFalse(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY }, +911 new double[] { Double.NEGATIVE_INFINITY })); +912 Assert.assertFalse(MathArrays.equals(new double[] { 1d }, +913 new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) })); +914 +915 } +916 +917 @Test +918 public void testArrayEqualsIncludingNaN() { +919 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, null)); +920 Assert.assertFalse(MathArrays.equalsIncludingNaN(null, new double[] { 1d })); +921 Assert.assertTrue(MathArrays.equalsIncludingNaN((double[]) null, (double[]) null)); +922 +923 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[0])); +924 Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[] { 1d })); +925 Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] { Double.NaN, Double.POSITIVE_INFINITY, +926 Double.NEGATIVE_INFINITY, 1d, 0d }, +927 new double[] { Double.NaN, Double.POSITIVE_INFINITY, +928 Double.NEGATIVE_INFINITY, 1d, 0d })); +929 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { Double.POSITIVE_INFINITY }, +930 new double[] { Double.NEGATIVE_INFINITY })); +931 Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, +932 new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) })); +933 } +934 +935 @Test +936 public void testNormalizeArray() { +937 double[] testValues1 = new double[] {1, 1, 2}; +938 TestUtils.assertEquals( new double[] {.25, .25, .5}, +939 MathArrays.normalizeArray(testValues1, 1), +940 Double.MIN_VALUE); +941 +942 double[] testValues2 = new double[] {-1, -1, 1}; +943 TestUtils.assertEquals( new double[] {1, 1, -1}, +944 MathArrays.normalizeArray(testValues2, 1), +945 Double.MIN_VALUE); +946 +947 // Ignore NaNs +948 double[] testValues3 = new double[] {-1, -1, Double.NaN, 1, Double.NaN}; +949 TestUtils.assertEquals( new double[] {1, 1,Double.NaN, -1, Double.NaN}, +950 MathArrays.normalizeArray(testValues3, 1), +951 Double.MIN_VALUE); +952 +953 // Zero sum -> MathArithmeticException +954 double[] zeroSum = new double[] {-1, 1}; +955 try { +956 MathArrays.normalizeArray(zeroSum, 1); +957 Assert.fail("expecting MathArithmeticException"); +958 } catch (MathArithmeticException ex) {} +959 +960 // Infinite elements -> MathArithmeticException +961 double[] hasInf = new double[] {1, 2, 1, Double.NEGATIVE_INFINITY}; +962 try { +963 MathArrays.normalizeArray(hasInf, 1); +964 Assert.fail("expecting MathIllegalArgumentException"); +965 } catch (MathIllegalArgumentException ex) {} +966 +967 // Infinite target -> MathIllegalArgumentException +968 try { +969 MathArrays.normalizeArray(testValues1, Double.POSITIVE_INFINITY); +970 Assert.fail("expecting MathIllegalArgumentException"); +971 } catch (MathIllegalArgumentException ex) {} +972 +973 // NaN target -> MathIllegalArgumentException +974 try { +975 MathArrays.normalizeArray(testValues1, Double.NaN); +976 Assert.fail("expecting MathIllegalArgumentException"); +977 } catch (MathIllegalArgumentException ex) {} +978 } +979 +980 @Test +981 public void testConvolve() { +982 /* Test Case (obtained via SciPy) +983 * x=[1.2,-1.8,1.4] +984 * h=[1,0.8,0.5,0.3] +985 * convolve(x,h) -> array([ 1.2 , -0.84, 0.56, 0.58, 0.16, 0.42]) +986 */ +987 double[] x1 = { 1.2, -1.8, 1.4 }; +988 double[] h1 = { 1, 0.8, 0.5, 0.3 }; +989 double[] y1 = { 1.2, -0.84, 0.56, 0.58, 0.16, 0.42 }; +990 double tolerance = 1e-13; +991 +992 double[] yActual = MathArrays.convolve(x1, h1); +993 Assert.assertArrayEquals(y1, yActual, tolerance); +994 +995 double[] x2 = { 1, 2, 3 }; +996 double[] h2 = { 0, 1, 0.5 }; +997 double[] y2 = { 0, 1, 2.5, 4, 1.5 }; +998 +999 yActual = MathArrays.convolve(x2, h2); +1000 Assert.assertArrayEquals(y2, yActual, tolerance); +1001 +1002 try { +1003 MathArrays.convolve(new double[]{1, 2}, null); +1004 Assert.fail("an exception should have been thrown"); +1005 } catch (NullArgumentException e) { +1006 // expected behavior +1007 } +1008 +1009 try { +1010 MathArrays.convolve(null, new double[]{1, 2}); +1011 Assert.fail("an exception should have been thrown"); +1012 } catch (NullArgumentException e) { +1013 // expected behavior +1014 } +1015 +1016 try { +1017 MathArrays.convolve(new double[]{1, 2}, new double[]{}); +1018 Assert.fail("an exception should have been thrown"); +1019 } catch (NoDataException e) { +1020 // expected behavior +1021 } +1022 +1023 try { +1024 MathArrays.convolve(new double[]{}, new double[]{1, 2}); [... 248 lines stripped ...]