<commit_before>include "numpy.pxd"


#define PYERR(message) do {PyErr_SetString(PyExc_ValueError, message); goto fail;} while(0)
#define DATA(arr) PyArray_DATA(arr)
#define DIMS(arr) PyArray_DIMS(arr)
#define STRIDES(arr) PyArray_STRIDES(arr)
#define ELSIZE(arr) (PyArray_DESCR(arr)->elsize)
#define OBJECTTYPE(arr) PyArray_TYPE(arr)
#define BASEOBJ(arr) PyArray_BASE(arr)
#define RANK(arr) PyArray_NDIM(arr)
#define ISCONTIGUOUS(m) (PyArray_FLAGS(m) & NPY_CONTIGUOUS)


cdef extern from "S_bspline_util.h":
    int S_cubic_spline2D(float*, float*, int, int, double, npy_intp*,
                         npy_intp*,float)
    int S_quadratic_spline2D(float*, float*, int, int, double, npy_intp*,
                             npy_intp*, float)
    int S_IIR_forback1(float, float, float*, float*, int, int, int, float)
    int S_IIR_forback2(double, double, float*, float*, int, int, int, float)
    int S_separable_2Dconvolve_mirror(float*, float*, int, int, float*, float*,
                                      int, int, npy_intp*, npy_intp*)

cdef extern from "D_bspline_util.h":
    int D_cubic_spline2D(double*, double*, int, int, double, npy_intp*,
                         npy_intp*, double)
    int D_quadratic_spline2D(double*, double*, int, int, double, npy_intp*,
                             npy_intp*, double)
    int D_IIR_forback1(double, double, double*, double*, int, int, int, double)
    int D_IIR_forback2(double, double, double*, double*, int, int, int, double) 
    int D_separable_2Dconvolve_mirror(double*, double*, int, int, double*,
                                      double*, int, int, npy_intp*, npy_intp*)


cdef void convert_strides(npy_intp* instrides,
                          npy_intp* convstrides,
                          int size, int N):
    cdef int n
    cdef npy_intp bitshift

    bitshift = -1;
    while size != 0:
        size >>= 1;
        bitshift += 1;

    for n from 0 <= n < N:
        convstrides[n] = instrides[n] >> bitshift



static char doc_cspline2d[] = "cspline2d(input {, lambda, precision}) -> ck\n"
"\n"
"  Description:\n"
"\n"
"    Return the third-order B-spline coefficients over a regularly spacedi\n" 
"    input grid for the two-dimensional input image.  The lambda argument\n" 
"    specifies the amount of smoothing.  The precision argument allows specifying\n"
"    the precision used when computing the infinite sum needed to apply mirror-\n"
"    symmetric boundary conditions.\n";

 
static PyObject *cspline2d(PyObject *NPY_UNUSED(dummy), PyObject *args)
{
  PyObject *image=NULL;
  PyArrayObject *a_image=NULL, *ck=NULL;
  double lambda = 0.0;
  double precision = -1.0;
  int thetype, M, N, retval=0;
  npy_intp outstrides[2], instrides[2];

  if (!PyArg_ParseTuple(args, "O|dd", &image, &lambda, &precision)) return NULL;

  thetype = PyArray_ObjectType(image, PyArray_FLOAT);
  thetype = NPY_MIN(thetype, PyArray_DOUBLE);
  a_image = (PyArrayObject *)PyArray_FromObject(image, thetype, 2, 2);
  if (a_image == NULL) goto fail;
 
  ck = (PyArrayObject *)PyArray_SimpleNew(2,DIMS(a_image),thetype);
  if (ck == NULL) goto fail;
  M = DIMS(a_image)[0];
  N = DIMS(a_image)[1];

  convert_strides(STRIDES(a_image), instrides, ELSIZE(a_image), 2);
  outstrides[0] = N;
  outstrides[1] = 1;

  if (thetype == PyArray_FLOAT) {
    if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-3;
    retval = S_cubic_spline2D((float *)DATA(a_image), (float *)DATA(ck), M, N, lambda, instrides, outstrides, precision);
  }
  else if (thetype == PyArray_DOUBLE) {
    if ((precision <= 0.0) || (precision > 1.0)) precision = 1e-6;
    retval = D_cubic_spline2D((double *)DATA(a_image), (double *)DATA(ck), M, N, lambda, instrides, outstrides, precision);
  }

  if (retval == -3) PYERR("Precision too high.  Error did not converge.");
  if (retval < 0) PYERR("Problem occurred inside routine");

  Py_DECREF(a_image);
  return PyArray_Return(ck);
 
 fail:
  Py_XDECREF(a_image);
  Py_XDECREF(ck);
  return NULL;

}

static char doc_qspline2d[] = "qspline2d(input {, lambda, precision}) -> qk\n"
"\n"
"  Description:\n"
"\n"
"    Return the second-order B-spline coefficients over a regularly spaced\n"
"    input grid for the two-dimensional input image.  The lambda argument\n" 
"    specifies the amount of smoothing.  The precision argument allows specifying\n"
"    the precision used when computing the infinite sum needed to apply mirror-\n"
"    symmetric boundary conditions.\n";
 
static PyObject *qspline2d(PyObject *NPY_UNUSED(dummy), PyObject *args)<commit_msg>cythonized cspline2d()<commit_after>include "numpy.pxd"


#define PYERR(message) do {PyErr_SetString(PyExc_ValueError, message); goto fail;} while(0)
#define DATA(arr) PyArray_DATA(arr)
#define DIMS(arr) PyArray_DIMS(arr)
#define STRIDES(arr) PyArray_STRIDES(arr)
#define ELSIZE(arr) (PyArray_DESCR(arr)->elsize)
#define OBJECTTYPE(arr) PyArray_TYPE(arr)
#define BASEOBJ(arr) PyArray_BASE(arr)
#define RANK(arr) PyArray_NDIM(arr)
#define ISCONTIGUOUS(m) (PyArray_FLAGS(m) & NPY_CONTIGUOUS)


cdef extern from "S_bspline_util.h":
    int S_cubic_spline2D(float*, float*, int, int, double, npy_intp*,
                         npy_intp*,float)
    int S_quadratic_spline2D(float*, float*, int, int, double, npy_intp*,
                             npy_intp*, float)
    int S_IIR_forback1(float, float, float*, float*, int, int, int, float)
    int S_IIR_forback2(double, double, float*, float*, int, int, int, float)
    int S_separable_2Dconvolve_mirror(float*, float*, int, int, float*, float*,
                                      int, int, npy_intp*, npy_intp*)

cdef extern from "D_bspline_util.h":
    int D_cubic_spline2D(double*, double*, int, int, double, npy_intp*,
                         npy_intp*, double)
    int D_quadratic_spline2D(double*, double*, int, int, double, npy_intp*,
                             npy_intp*, double)
    int D_IIR_forback1(double, double, double*, double*, int, int, int, double)
    int D_IIR_forback2(double, double, double*, double*, int, int, int, double) 
    int D_separable_2Dconvolve_mirror(double*, double*, int, int, double*,
                                      double*, int, int, npy_intp*, npy_intp*)


cdef void convert_strides(npy_intp* instrides,
                          npy_intp* convstrides,
                          int size, int N):
    cdef int n
    cdef npy_intp bitshift

    bitshift = -1
    while size != 0:
        size >>= 1
        bitshift += 1

    for n from 0 <= n < N:
        convstrides[n] = instrides[n] >> bitshift


def cspline2d(dummy, image, lambda_=0.0, precision=-1.0):
    """cspline2d(input {, lambda, precision}) -> ck

    Description:

    Return the third-order B-spline coefficients over a regularly spacedi
    input grid for the two-dimensional input image.  The lambda argument
    specifies the amount of smoothing.  The precision argument allows specifying
    the precision used when computing the infinite sum needed to apply mirror-
    symmetric boundary conditions.
    """
    cdef int thetype, M, N, retval=0
    cdef npy_intp outstrides[2], instrides[2]

    thetype = PyArray_ObjectType(image, PyArray_FLOAT)
    thetype = NPY_MIN(thetype, PyArray_DOUBLE)
    a_image = <PyArrayObject *> PyArray_FromObject(image, thetype, 2, 2)
 
    ck = <PyArrayObject *> PyArray_SimpleNew(2, DIMS(a_image),thetype)
    M = DIMS(a_image)[0]
    N = DIMS(a_image)[1]

    convert_strides(STRIDES(a_image), instrides, ELSIZE(a_image), 2)
    outstrides[0] = N
    outstrides[1] = 1

    if thetype == PyArray_FLOAT:
        if not (0.0 <= precision < 1.0):
            precision = 1e-3
        return S_cubic_spline2D(<float *> DATA(a_image),
                                <float *> DATA(ck),
                                M, N, lambda_, instrides, outstrides,
                                precision)

    elif thetype == PyArray_DOUBLE:
        if not (0.0 <= precision < 1.0):
            precision = 1e-6;
        return D_cubic_spline2D(<double *> DATA(a_image),
                                <double *> DATA(ck),
                                M, N, lambda_, instrides, outstrides,
                                precision);

    if retval == -3:
        raise Exeption("Precision too high.  Error did not converge.")

    if (retval < 0):
        raise Exeption("Problem occurred inside routine")


static char doc_qspline2d[] = "qspline2d(input {, lambda, precision}) -> qk\n"
"\n"
"  Description:\n"
"\n"
"    Return the second-order B-spline coefficients over a regularly spaced\n"
"    input grid for the two-dimensional input image.  The lambda argument\n" 
"    specifies the amount of smoothing.  The precision argument allows specifying\n"
"    the precision used when computing the infinite sum needed to apply mirror-\n"
"    symmetric boundary conditions.\n";
 
static PyObject *qspline2d(PyObject *NPY_UNUSED(dummy), PyObject *args)
{'fraction_non_alphanumeric': 0.11775280898876404, 'fraction_numerical': 0.012134831460674157, 'mean_word_length': 3.062528525787312, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '900384', 'n_tokens_mistral': 3141, 'n_tokens_neox': 3048, 'n_words': 805}