title: Generalized Universal Function in numpy
body: <p>I'm trying make a generalized ufunc using numpy API. The inputs are one <code>(n x m)</code> matrix and a scalar, and outputs are two matrix (<code>(n x p)</code> and <code>(p x m)</code>). But I don't knowing how to do it. Someone could help me?
  In init function I'm using <code>PyUFunc_FromFuncAndDataAndSignature</code> function with signature: </p>
  
  <pre><code>"(n,m),()-&gt;(n,p),(p,m)"
  </code></pre>
  
  <p>I can read the inputs (matrix and scalar), but I wanted to use the scalar input like the dimension p in signature. Is it possible?</p>
  
  <p>Here a example code that just print the inputs:</p>
  
  <pre><code>#include "Python.h"
  #include "math.h"
  #include "numpy/ndarraytypes.h"
  #include "numpy/ufuncobject.h"
  
  static PyMethodDef nmfMethods[] = {
          {NULL, NULL, 0, NULL}
  };
  
  
  static void double_nmf(char **args, npy_intp *dimensions,
                              npy_intp* steps, void* data)
  {
      npy_intp i, j, 
               n = dimensions[1], //dimensions of input matrix
               m = dimensions[2]; //
  
      printf("scalar: %d\n",*(int*)args[1]); // input scalar
  
      // just print input matrix
      printf("Input matrix:\n");
      for(i=0;i&lt;n;i++){
          for(j=0;j&lt;m;j++){
              printf("%.1f ",*(double*)(args[0]+8*(i*m+j)));
          }
      printf("\n");
      }
      return;
  
  }
  
  static PyUFuncGenericFunction nmf_functions[] = { double_nmf };
  static void * nmf_data[] = { (void *)NULL };
  static char nmf_signatures[] = { PyArray_DOUBLE, PyArray_INT, PyArray_DOUBLE, PyArray_DOUBLE };
  char *nmf_signature = "(n,m),()-&gt;(n,p),(p,m)";
  
  PyMODINIT_FUNC initnmf(void)
  {
      PyObject *m, *d, *version, *nmf;
  
      m = Py_InitModule("nmf", nmfMethods);
      if (m == NULL) {
          return;
      }
  
      import_array();
      import_umath();
      d = PyModule_GetDict(m);
      version = PyString_FromString("0.1");
      PyDict_SetItemString(d, "__version__", version);
      Py_DECREF(version);
  
      nmf = PyUFunc_FromFuncAndDataAndSignature(nmf_functions, nmf_data, nmf_signatures, 1,
                                      2, 2, PyUFunc_None, "nmf",
                                      "", 0, nmf_signature);
      PyDict_SetItemString(d, "nmf", nmf);
      Py_DECREF(nmf);
  }
  </code></pre>
  
  <p>This code compiles and works. The python script is here:</p>
  
  <pre><code>#/usr/bin/python
  
  import numpy as np
  import nmf
  
  x = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]])
  y,z = nmf.nmf(x,2)
  print "Shapes of outputs: ", y.shape, z.shape
  </code></pre>
  
  <p>And the terminal output is:</p>
  
  <pre><code>scalar: 2
  Input matrix:
  1.0 2.0 3.0 4.0 5.0 
  6.0 7.0 8.0 9.0 10.0 
  11.0 12.0 13.0 14.0 15.0 
  16.0 17.0 18.0 19.0 20.0 
  Shapes of outputs:  (4, 1) (1, 5)
  </code></pre>
  
  <p>My doubt is how use the scalar input (2 in the case) like dimension p of outputs matrices. In example p = 1, and I don't set it.</p>