Repository: scipy/scipy
Pull request: MAINT: `special.gegenbauer`: fix behavior for `n=0`; add input validation of `alpha`
#### Reference issue
Closes gh-8888

#### What does this implement/fix?
gh-8888 noted that `special.gegenbauer(0, 0)` produced a NaN polynomial instead of the polynomial $1$. This fixes the bug and adds input validation for the `alpha` parameter.

#### Additional information
It would not be hard to find adjacent rough edges. Let's not go down that rabbit hole here.

Problem statement:
MAINT: special.gegenbauer: case of `alpha = 0` not handled
scipy.special.gegenbauer does not handle case alpha = 0. E.g. $C_0^0(z)$ should be 1 (evaluated as nan poly)


### Reproducing code example:
```
from scipy.special import gegenbauer as C
C(0, 0)
```
### Error message:
```
/usr/lib64/python3.5/site-packages/scipy/special/orthogonal.py:1351: RuntimeWarning: invalid value encountered in double_scalars
  _gam(2*alpha) / _gam(alpha + 0.5 + n))
Out[263]: poly1d([nan])
```

### Scipy/Numpy/Python version information:
```
1.1.0 1.14.3 sys.version_info(major=3, minor=5, micro=5, releaselevel='final', serial=0)
```

Hints from issue discussion:

Commit message: MAINT: special.gegenbauer: add input validation of alpha

Patch to review:
diff --git a/scipy/special/_orthogonal.py b/scipy/special/_orthogonal.py
index 031f68c6abc7..e021f5a899b2 100644
--- a/scipy/special/_orthogonal.py
+++ b/scipy/special/_orthogonal.py
@@ -1616,8 +1616,10 @@ def gegenbauer(n, alpha, monic=False):
     >>> plt.show()
 
     """
+    if not np.isfinite(alpha) or alpha <= -0.5 :
+        raise ValueError("`alpha` must be a finite number greater than -1/2")
     base = jacobi(n, alpha - 0.5, alpha - 0.5, monic=monic)
-    if monic:
+    if monic or n == 0:
         return base
     #  Abrahmowitz and Stegan 22.5.20
     factor = (_gam(2*alpha + n) * _gam(alpha + 0.5) /
diff --git a/scipy/special/tests/test_orthogonal.py b/scipy/special/tests/test_orthogonal.py
index 5b644a62c0b6..c3f3caeb23e6 100644
--- a/scipy/special/tests/test_orthogonal.py
+++ b/scipy/special/tests/test_orthogonal.py
@@ -1,8 +1,10 @@
+import pytest
+from pytest import raises as assert_raises
+
 import numpy as np
 from numpy import array, sqrt
 from numpy.testing import (assert_array_almost_equal, assert_equal,
                            assert_almost_equal, assert_allclose)
-from pytest import raises as assert_raises
 
 from scipy import integrate
 import scipy.special as sc
@@ -93,6 +95,21 @@ def test_gegenbauer(self):
         assert_array_almost_equal(Ca5.c,array([4*sc.poch(a,5),0,-20*sc.poch(a,4),
                                                0,15*sc.poch(a,3),0])/15.0,11)
 
+    def test_n_zero_gh8888(self):
+        # gh-8888 reported that gegenbauer(0, 0) returns NaN polynomial
+        Ca0 = orth.gegenbauer(0, 0)
+        assert_equal(Ca0.c, np.asarray([1.]))
+
+    def test_valid_alpha(self):
+        # Check input validation of `alpha`
+        message = '`alpha` must be a finite number greater...'
+        with pytest.raises(ValueError, match=message):
+            orth.gegenbauer(0, np.nan)
+        with pytest.raises(ValueError, match=message):
+            orth.gegenbauer(1, -0.5)
+        with pytest.raises(ValueError, match=message):
+            orth.gegenbauer(2, -np.inf)
+
 
 class TestHermite:
     def test_hermite(self):


Code review comments:
diff_hunk: @@ -93,6 +95,21 @@ def test_gegenbauer(self):
         assert_array_almost_equal(Ca5.c,array([4*sc.poch(a,5),0,-20*sc.poch(a,4),
                                                0,15*sc.poch(a,3),0])/15.0,11)
 
+    def test_n_zero_gh8888(self):
+        # gh-8888 reported that gegenbauer(0, 0) returns NaN polynomial
+        Ca0 = orth.gegenbauer(0, 0)
+        assert_equal(Ca0.c, np.asarray([1.]))
path: scipy/special/tests/test_orthogonal.py
text: @author:
```suggestion
    @user1.mark.parametrize('a', [0, 1])
    def test_n_zero_gh8888(self, a):
        # gh-8888 reported that gegenbauer(0, 0) returns NaN polynomial
        Cn0 = orth.gegenbauer(0, a)
        assert_equal(Cn0.c, np.asarray([1.]))
```
How about this @user2-bowhay?