PrivateFunction

The PrivateFunction node allows you to implement your own C function and use this as a function node. The function is defined in a shared library that is opened on the fly. Required Fields:

* lib_name The name and path of your shared library

* function_name the name of the function to be used.

Format of your C library

The function should be of the form:

double test(const double *par,const double *x){
  return (par[0] + par[1])*x[1];
}

where test is your function_name, par are the results of the children of your node and x are the extra Axes.

Furthermore, two constants should be defined:

const int Npar_test=2;
const int Nx_test = 2;

Npar_<function_name> is the number of parameters of your function, i.e. the number of children of your node. Nx_<function_name> the largest index of the used axes +1, or zero if no x is used.

It is also possible to define the complex equivalent of your function:

#include <complex.h>
complex test_complex(const complex *par,const complex *x){
  return (par[0] + par[1])*x[1];
}  

If one or more of the children returns a complex result, this complex function will be used for evaluating the result. If no such complex function exists it will result in a Fail.

The possibility to convert a real childresult into a complex and vice versa does not exist yet.

Example

Copy the following in a file test.c:

#include <math.h>
#include <complex.h>
double sqr(double x){
  return x*x;
}
double test_double(const double *par,const double *x){
  return (par[0] + par[1]);
  }
complex test_complex(const complex *par,const complex *x){
  return (par[0] + par[1]);
  }
int Npar_test=2;
int Nx_test=0;

and in the file Makefile:

slib:   test.o
        ld  -shared  test.o -o test.so
test.o: test.c
        gcc -c  -fpic test.c

now make your shared library:

$ make slib

Example of a tdl-script to test this pretty useless function:

from Timba.TDL import *
from Timba.Meq import meq
Settings.forest_state.cache_policy = 100;
Settings.forest_state.a = None;
Settings.orphans_are_roots = True;
def _define_forest (ns,**kwargs):
  child1 = Meq.ToComplex(1.,1.);
  child2 = Meq.ToComplex(2.,2.);

  priv = ns.priv<<Meq.PrivateFunction(children=(child1,child2),lib_name = "test.so",function_name ="test");
  add= Meq.Add(child1,child2);
  sub = ns.sub << Meq.Subtract(add,priv);

def _test_forest (mqs,parent,**kwargs):
  dom = meq.domain(0,3,0,1);
  cells = meq.cells(dom,num_freq=6,num_time=4);
  request = meq.request(cells,rqtype='ev');
  a = mqs.meq('Node.Execute',record(name='sub',request=request),wait=False);

PrivateFunction (last edited 2007-05-02 10:52:09 by MaaijkeMevius)