Skip to content

Add complex number support to sinh - #456

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

Add complex number support to sinh#456
kgryte merged 3 commits into
mainfrom
cmplx-sinh

Conversation

@kgryte

@kgrytekgryte commented Jun 20, 2022

Copy link
Copy Markdown
Contributor

This PR

  • adds complex number support to sinh by documenting special cases. The hyperbolic sine 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(0.0, 0.0), # 0complex(-0.0, -0.0), # 1complex(-0.0, -0.0), # 1complex(0.0, np.inf), # 2complex(0.0, np.nan), # 2complex(-0.0, np.inf), # 3complex(-0.0, np.nan), # 3complex(0.0, np.nan), # 4complex(0.0, np.nan), # 4complex(-0.0, np.nan), # 5complex(-0.0, np.nan), # 5complex(1.0, np.inf), # 6complex(np.nan, np.nan), # 6complex(1.0, np.nan), # 7complex(np.nan, np.nan), # 7complex(np.inf, 0.0), # 8complex(np.inf, 0.0), # 8complex(np.inf, -0.0), # 9complex(np.inf, -0.0), # 9complex(np.inf, 1.0), # 10complex(np.inf, np.inf), # 10complex(np.inf, np.inf), # 11complex(np.inf, np.nan), # 11complex(np.inf, np.nan), # 12complex(np.inf, np.nan), # 12complex(np.nan, 0.0), # 13complex(np.nan, 0.0), # 13complex(np.nan, 1.0), # 14complex(np.nan, np.nan), # 14complex(np.nan, 1.0), # 15complex(np.nan, np.nan), # 15complex(np.nan, np.nan), # 16complex(np.nan, np.nan) # 16
]
foriinrange(len(values)//2):
j=i*2v=values[j]
e=values[j+1]
actual=np.sinh(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')

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

Copy link
Copy Markdown
ContributorAuthor

As sinh 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 39911e8 into mainJul 7, 2022
@kgryte
kgryte deleted the cmplx-sinh branch July 7, 2022 09:49
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