def _first_and_last_element(arr):
 if (isinstance(arr, np.ndarray) or hasattr(arr, 'data')):
  data = (arr.data if sparse.issparse(arr) else arr)
  return (data.flat[0], data.flat[(-1)])
 else:
  return (arr[(0, 0)], arr[((-1), (-1))])

def _first_and_last_element ( arr ) : if ( isinstance ( arr , np . ndarray ) or hasattr ( arr , 'data' ) ) : data = ( arr . data if sparse . issparse ( arr ) else arr ) return ( data . flat [ 0 ] , data . flat [ ( - 1 ) ] ) else : return ( arr [ ( 0 , 0 ) ] , arr [ ( ( - 1 ) , ( - 1 ) ) ] )
returns first and last element of numpy array or sparse matrix .