@@ -51,6 +51,19 @@ def _(col):
5151 return _
5252
5353
54+ def _create_binary_mathfunction (name , doc = "" ):
55+ """ Create a binary mathfunction by name"""
56+ def _ (col1 , col2 ):
57+ sc = SparkContext ._active_spark_context
58+ # users might write ints for simplicity. This would throw an error on the JVM side.
59+ jc = getattr (sc ._jvm .functions , name )(col1 ._jc if isinstance (col1 , Column ) else float (col1 ),
60+ col2 ._jc if isinstance (col2 , Column ) else float (col2 ))
61+ return Column (jc )
62+ _ .__name__ = name
63+ _ .__doc__ = doc
64+ return _
65+
66+
5467_functions = {
5568 'lit' : 'Creates a :class:`Column` of literal value.' ,
5669 'col' : 'Returns a :class:`Column` based on the given column name.' ,
@@ -63,6 +76,34 @@ def _(col):
6376 'sqrt' : 'Computes the square root of the specified float value.' ,
6477 'abs' : 'Computes the absolute value.' ,
6578
79+ # unary math functions
80+ 'acos' : 'Computes the cosine inverse of the given value; the returned angle is in the range' +
81+ '0.0 through pi.' ,
82+ 'asin' : 'Computes the sine inverse of the given value; the returned angle is in the range' +
83+ '-pi/2 through pi/2.' ,
84+ 'atan' : 'Computes the tangent inverse of the given value.' ,
85+ 'cbrt' : 'Computes the cube-root of the given value.' ,
86+ 'ceil' : 'Computes the ceiling of the given value.' ,
87+ 'cos' : 'Computes the cosine of the given value.' ,
88+ 'cosh' : 'Computes the hyperbolic cosine of the given value.' ,
89+ 'exp' : 'Computes the exponential of the given value.' ,
90+ 'expm1' : 'Computes the exponential of the given value minus one.' ,
91+ 'floor' : 'Computes the floor of the given value.' ,
92+ 'log' : 'Computes the natural logarithm of the given value.' ,
93+ 'log10' : 'Computes the logarithm of the given value in Base 10.' ,
94+ 'log1p' : 'Computes the natural logarithm of the given value plus one.' ,
95+ 'rint' : 'Returns the double value that is closest in value to the argument and' +
96+ ' is equal to a mathematical integer.' ,
97+ 'signum' : 'Computes the signum of the given value.' ,
98+ 'sin' : 'Computes the sine of the given value.' ,
99+ 'sinh' : 'Computes the hyperbolic sine of the given value.' ,
100+ 'tan' : 'Computes the tangent of the given value.' ,
101+ 'tanh' : 'Computes the hyperbolic tangent of the given value.' ,
102+ 'toDegrees' : 'Converts an angle measured in radians to an approximately equivalent angle ' +
103+ 'measured in degrees.' ,
104+ 'toRadians' : 'Converts an angle measured in degrees to an approximately equivalent angle ' +
105+ 'measured in radians.' ,
106+
66107 'max' : 'Aggregate function: returns the maximum value of the expression in a group.' ,
67108 'min' : 'Aggregate function: returns the minimum value of the expression in a group.' ,
68109 'first' : 'Aggregate function: returns the first value in a group.' ,
@@ -74,10 +115,21 @@ def _(col):
74115 'sumDistinct' : 'Aggregate function: returns the sum of distinct values in the expression.' ,
75116}
76117
118+ # math functions that take two arguments as input
119+ _binary_mathfunctions = {
120+ 'atan2' : 'Returns the angle theta from the conversion of rectangular coordinates (x, y) to' +
121+ 'polar coordinates (r, theta).' ,
122+ 'hypot' : 'Computes `sqrt(a^2^ + b^2^)` without intermediate overflow or underflow.' ,
123+ 'pow' : 'Returns the value of the first argument raised to the power of the second argument.'
124+ }
125+
77126for _name , _doc in _functions .items ():
78127 globals ()[_name ] = _create_function (_name , _doc )
128+ for _name , _doc in _binary_mathfunctions .items ():
129+ globals ()[_name ] = _create_binary_mathfunction (_name , _doc )
79130del _name , _doc
80131__all__ += _functions .keys ()
132+ __all__ += _binary_mathfunctions .keys ()
81133__all__ .sort ()
82134
83135
0 commit comments