Skip to content

Add complex number support to cosh - #453

Merged
kgryte merged 3 commits into
mainfrom
cmplx-cosh
Jul 7, 2022
Merged

Add complex number support to cosh#453
kgryte merged 3 commits into
mainfrom
cmplx-cosh

Conversation

@kgryte

@kgrytekgryte commented Jun 13, 2022

Copy link
Copy Markdown
Contributor

This PR

  • adds complex number support to cosh by documenting special cases. The hyperbolic cosine is an entire function in the complex plane. Thus, the function has no branch cuts.
  • updates the input and output array data types to be any floating-point data type, not just real-valued floating-point data types.
  • derives special cases from C99 and tested against NumPy (script found below).
Details
importnumpyasnpimportmathdefis_equal_float(x, y):
"""Test whether two floating-point numbers are equal with special consideration for zeros and NaNs. Parameters ---------- x : float First input number. y : float Second input number. Returns ------- bool Boolean indicating whether two floating-point numbers are equal. Examples -------- >>> is_equal_float(0.0, -0.0) False >>> is_equal_float(-0.0, -0.0) True """# Handle +-0:ifx==0.0andy==0.0:
returnmath.copysign(1.0, x) ==math.copysign(1.0, y)
# Handle NaNs:ifx!=x:
returny!=y# Everything else, including infinities:returnx==ydefis_equal(x, y):
"""Test whether two complex numbers are equal with special consideration for zeros and NaNs. Parameters ---------- x : complex First input number. y : complex Second input number. Returns ------- bool Boolean indicating whether two complex numbers are equal. Examples -------- >>> import numpy as np >>> is_equal(complex(np.nan, np.nan), complex(np.nan, np.nan)) True """returnis_equal_float(x.real, y.real) andis_equal_float(x.imag, y.imag)
# Strided array consisting of input values and expected values:values= [
complex(0.0, 0.0), # 0complex(1.0, 0.0), # 0complex(-0.0, 0.0), # 1complex(1.0, -0.0), # 1complex(0.0, np.inf), # 2complex(np.nan, 0), # 2complex(0.0, np.nan), # 3complex(np.nan, 0.0), # 3complex(1.0, np.inf), # 4complex(np.nan, np.nan), # 4complex(1.0, np.nan), # 5complex(np.nan, np.nan), # 5complex(np.inf, 0.0), # 6complex(np.inf, 0.0), # 6complex(np.inf, -0.0), # 7complex(np.inf, -0.0), # 7complex(np.inf, 1.0), # 8complex(np.inf, np.inf), # 8complex(np.inf, np.inf), # 9complex(np.inf, np.nan), # 9complex(np.inf, np.nan), # 10complex(np.inf, np.nan), # 10complex(np.nan, 0.0), # 11complex(np.nan, 0.0), # 11complex(np.nan, 1.0), # 12complex(np.nan, np.nan), # 12complex(np.nan, 1.0), # 13complex(np.nan, np.nan), # 13complex(np.nan, np.nan), # 14complex(np.nan, np.nan) # 14
]
foriinrange(len(values)//2):
j=i*2v=values[j]
e=values[j+1]
actual=np.cosh(v)
print('Value: {value}'.format(value=str(v)))
print('Actual: {actual}'.format(actual=str(actual)))
print('Expected: {expected}'.format(expected=str(e)))
print('Equal: {is_equal}'.format(is_equal=str(is_equal(actual, e))))
print('\n')
Value: 0j
Actual: (1+0j)
Expected: (1+0j)
Equal: True
Value: (-0+0j)
Actual: (1-0j)
Expected: (1-0j)
Equal: True
/path/to/ccosh.py:116: RuntimeWarning: invalid value encountered in cosh
actual = np.cosh(v)
Value: infj
Actual: (nan+0j)
Expected: (nan+0j)
Equal: True
Value: nanj
Actual: (nan+0j)
Expected: (nan+0j)
Equal: True
Value: (1+infj)
Actual: (nan+nanj)
Expected: (nan+nanj)
Equal: True
Value: (1+nanj)
Actual: (nan+nanj)
Expected: (nan+nanj)
Equal: True
Value: (inf+0j)
Actual: (inf+0j)
Expected: (inf+0j)
Equal: True
Value: (inf-0j)
Actual: (inf-0j)
Expected: (inf-0j)
Equal: True
Value: (inf+1j)
Actual: (inf+infj)
Expected: (inf+infj)
Equal: True
Value: (inf+infj)
Actual: (inf+nanj)
Expected: (inf+nanj)
Equal: True
Value: (inf+nanj)
Actual: (inf+nanj)
Expected: (inf+nanj)
Equal: True
Value: (nan+0j)
Actual: (nan+0j)
Expected: (nan+0j)
Equal: True
Value: (nan+1j)
Actual: (nan+nanj)
Expected: (nan+nanj)
Equal: True
Value: (nan+1j)
Actual: (nan+nanj)
Expected: (nan+nanj)
Equal: True
Value: (nan+nanj)
Actual: (nan+nanj)
Expected: (nan+nanj)
Equal: True

@kgrytekgryte added API extension Adds new functions or objects to the API. topic: Complex Data Types Complex number data types. labels Jun 13, 2022
@kgrytekgryte added this to the v2022 milestone Jun 13, 2022
@kgrytekgryte added API change Changes to existing functions or objects in the API. and removed API extension Adds new functions or objects to the API. labels Jun 20, 2022
@kgryte

Copy link
Copy Markdown
ContributorAuthor

As cosh does not involve branch cuts and the special cases introduced in this PR follow prior art, will merge. Further revisions can be addressed in subsequent PRs...

@kgryte
kgryte merged commit b43dcb1 into mainJul 7, 2022
@kgryte
kgryte deleted the cmplx-cosh branch July 7, 2022 09:46
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API changeChanges to existing functions or objects in the API.topic: Complex Data TypesComplex number data types.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@kgryte