예제 #1
0
파일: swig_int.cpp 프로젝트: lonnell/trick
PyObject * swig_int::__div__( PyObject * obj1 ) {
    swig_double * result ;
    PyObject * ret = NULL ;
    void * argp2 ;

    result = new swig_double() ;

    if ( PyFloat_Check(obj1) ) {
        result->value = value / PyFloat_AsDouble(obj1) ;
        result->units = units ;
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_int *"), 0 ))) {
        swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ;
        result->value = value / temp_m->value ;
        result->units = units + "/(" + temp_m->units + ")";
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) {
        swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ;
        result->value = value / temp_m->value ;
        result->units = units + "/(" + temp_m->units + ")";
    } else if ( PyInt_Check(obj1) ) {
        result->value = value / PyInt_AsLong(obj1) ;
        result->units = units ;
    }
    ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_double *"), SWIG_POINTER_OWN);
    return ret ;
}
예제 #2
0
파일: swig_int.cpp 프로젝트: lonnell/trick
PyObject * swig_int::__lshift__( PyObject * obj1 ) {
    swig_int * result ;
    PyObject * ret = NULL ;
    void * argp2 ;

    result = new swig_int() ;

    if ( PyFloat_Check(obj1) ) {
        PyErr_SetString(PyExc_TypeError,"Shift value must be integer");
        return NULL ;
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_int *"), 0 ))) {
        swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ;
        if ( !temp_m->units.compare("1")) {
            result->value = value << temp_m->value ;
        } else {
            PyErr_SetString(PyExc_TypeError,"Shift value must be unitless.");
            return NULL ;
        }
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) {
        PyErr_SetString(PyExc_TypeError,"Shift value must be integer");
        return NULL ;
    } else if ( PyInt_Check(obj1) ) {
        result->value = value << PyInt_AsLong(obj1) ;
    }
    result->units = units ;

    ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_int *"), SWIG_POINTER_OWN);

    return ret ;
}
예제 #3
0
파일: swig_int.cpp 프로젝트: lonnell/trick
PyObject * swig_int::__pow__( PyObject * obj1 ) {
    PyObject * ret = NULL ;
    void * argp2 ;

    if ( units.compare("1")) {
        PyErr_SetString(PyExc_TypeError,"Both arguments must be unitless. Cannot create new unit-ed type.");
        return NULL ;
    }

    if ( PyFloat_Check(obj1) ) {
        swig_double * result = new swig_double() ;
        result->value = pow(value , PyFloat_AsDouble(obj1)) ;
        result->units = units ;
        ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_double *"), SWIG_POINTER_OWN);
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_int *"), 0 ))) {
        swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ;
        swig_int * result = new swig_int() ;
        if ( !temp_m->units.compare("1")) {
            result->value = (long long)(pow(value , temp_m->value)) ;
        } else {
            PyErr_SetString(PyExc_TypeError,"Both arguments must be unitless. Cannot create new unit-ed type.");
            return NULL ;
        }
        ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_int *"), SWIG_POINTER_OWN);
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) {
        swig_double * result = new swig_double() ;
        swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ;
        if ( !temp_m->units.compare("1")) {
            result->value = pow(value , temp_m->value) ;
        } else {
            PyErr_SetString(PyExc_TypeError,"Both arguments must be unitless. Cannot create new unit-ed type.");
            return NULL ;
        }
        result->units = units ;
        ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_double *"), SWIG_POINTER_OWN);
    } else if ( PyInt_Check(obj1) ) {
        swig_int * result = new swig_int() ;
        int power = PyInt_AsLong(obj1) ;
        result->value = (long long)(pow(value , power)) ;
        result->units = units ;
        for ( int ii = 1 ; ii < power ; ii++ ) {
            result->units = result->units + "*(" + units + ")";
        }
        ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_int *"), SWIG_POINTER_OWN);
    }

    return ret ;
}
예제 #4
0
SWIGINTERNINLINE void *
SWIG_Guile_MustGetPtr (SCM s, swig_type_info *type,
		       int argnum, int flags, const char *func_name)
{
  void *result;
  int res = SWIG_Guile_ConvertPtr(s, &result, type, flags);
  if (!SWIG_IsOK(res)) {
    /* type mismatch */
    scm_wrong_type_arg((char *) func_name, argnum, s);
  }
  return result;
}
예제 #5
0
파일: swig_int.cpp 프로젝트: lonnell/trick
PyObject * swig_int::__mod__( PyObject * obj1 ) {
    PyObject * ret = NULL ;
    void * argp2 ;

    if ( PyFloat_Check(obj1) ) {
        swig_double * result = new swig_double() ;
        result->value = fmod(value , PyFloat_AsDouble(obj1)) ;
        result->units = units ;
        ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_double *"), SWIG_POINTER_OWN);
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_int *"), 0 ))) {
        swig_int * result = new swig_int() ;
        swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ;
        if ( !temp_m->units.compare("1")) {
            result->value = value % temp_m->value ;
        } else {
            PyErr_SetString(PyExc_TypeError,"Divisor must be unitless.  Cannot create new unit-ed type.");
            return NULL ;
        }
        result->units = units ;
        ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_int *"), SWIG_POINTER_OWN);
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) {
        swig_double * result = new swig_double() ;
        swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ;
        if ( !temp_m->units.compare("1")) {
            result->value = fmod( value , temp_m->value) ;
        } else {
            PyErr_SetString(PyExc_TypeError,"Divisor must be unitless.  Cannot create new unit-ed type.");
            return NULL ;
        }
        result->units = units ;
        ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_double *"), SWIG_POINTER_OWN);
    } else if ( PyInt_Check(obj1) ) {
        swig_int * result = new swig_int() ;
        result->value = value % PyInt_AsLong(obj1) ;
        result->units = units ;
        ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_int *"), SWIG_POINTER_OWN);
    }

    return ret ;
}
예제 #6
0
파일: swig_int.cpp 프로젝트: lonnell/trick
PyObject * swig_int::__sub__( PyObject * obj1 ) {
    swig_int * result ;
    PyObject * ret = NULL ;
    void * argp2 ;
    int conv_ret ;

    result = new swig_int() ;

    if ( PyFloat_Check(obj1) ) {
        result->value = (long long)(round(value - PyFloat_AsDouble(obj1))) ;
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_int *"), 0 ))) {
        long long new_val ;
        swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ;
        new_val = temp_m->value ;
        conv_ret = scale_united_value( units , temp_m->units , &new_val ) ;
        if ( conv_ret == 0 ) {
            result->value = value - new_val ;
        } else {
            return NULL ;
        }
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_double *"), 0 ))) {
        double new_val ;
        swig_double * temp_m = reinterpret_cast< swig_double * >(argp2) ;
        new_val = temp_m->value ;
        conv_ret = scale_united_value( units , temp_m->units , &new_val ) ;
        if ( conv_ret == 0 ) {
            result->value = (long long)(round(value - new_val)) ;
        } else {
            return NULL ;
        }
    } else if ( PyInt_Check(obj1) ) {
        result->value = value - (long long)PyInt_AsLong(obj1) ;
    }
    result->units = units ;

    ret = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIG_TypeQuery("swig_int *"), SWIG_POINTER_OWN);

    return ret ;
}
예제 #7
0
bool PopPointer(lua_State* L, void** ptr, const char* type_name, int idx)
{
    swig_type_info * pTypeInfo = querySwigType(type_name);
    if (pTypeInfo == 0)
    {
        return false;  
    }

    if(!lua_isuserdata(L,idx))
    {
        return false;
    }

    return SWIG_IsOK(SWIG_ConvertPtr(L, idx, ptr, pTypeInfo, 0));
}
예제 #8
0
파일: swig_int.cpp 프로젝트: lonnell/trick
PyObject * swig_int::__rshift__( PyObject * obj1 ) {
    swig_int * result ;
    PyObject * ret = NULL ;
    void * argp2 ;

    result = new swig_int() ;

    if ( PyFloat_Check(obj1) ) {
        PyErr_SetString(PyExc_TypeError,"Shift value must be integer");
        return NULL ;
    } else if (SWIG_IsOK(SWIG_ConvertPtr(obj1, &argp2,SWIG_TypeQuery("swig_int *"), 0 ))) {
        swig_int * temp_m = reinterpret_cast< swig_int * >(argp2) ;
        if ( !temp_m->units.compare("1")) {
            result->value = value >> temp_m->value ;
        } else {
static PyObject* GetAmsEntityList(PyObject *self, PyObject *args)
{
  ClAmsEntityListT *lst = (ClAmsEntityListT *) 0 ;
  void *argp1 = 0 ;

  if (!PyArg_ParseTuple(args,(char *)"O:GetAmsEntityListT",&obj0)) return NULL;
  res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_ClAmsEntityListT, 0 |  0 );
  if (!SWIG_IsOK(res1))
  {
    SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "GetAmsEntityList" "', argument " "1"" of type '" "ClAmsEntityListT *""'"); 
  }
  lst = (ClAmsEntityListT *)(argp1);

  char buf[2048];
  const int bufLen = 2048;
  int curLen = 0;
  
  if (lst->isValid == 0)
  {
      sprintf(buf,"List is not valid");
      PyErr_SetString(PyExc_SystemError,buf);
      return NULL; /* Oops, list is invalid */
  }
  

  ClCntNodeHandleT  nodeHandle = -1;

  curLen += snprintf(buf+curLen, bufLen-curLen, "[");

  for (int count=0, entityRef = (ClAmsEntityRefT *) clAmsCntGetFirst( &entityList->list,&nodeHandle);
       entityRef != NULL; 
       count++, entityRef = (ClAmsEntityRefT *) clAmsCntGetNext( &entityList->list,&nodeHandle) )
  {
      AMS_CHECKPTR_AND_EXIT (!entityRef);        
      curLen += snprintf(buf+curLen, bufLen-curLen, "%s(%d,'%.*s')",(count==0) ? "":",", entityRef->entity.type,entityRef->entity.name.length,entityRef->entity.name.value);
  }

  curLen += snprintf(buf+curLen, bufLen-curLen, "]");
  
  PyObject* ret = PyRun_String(buf,Py_eval_input,emptyDict,emptyDict);
  return ret;
}
예제 #10
0
파일: modpython.cpp 프로젝트: ZachBeta/znc
Csock* CPySocket::GetSockObj(const CString& sHost, unsigned short uPort) {
	CPySocket* result = NULL;
	PyObject* pyRes = PyObject_CallMethod(m_pyObj, const_cast<char*>("_Accepted"), const_cast<char*>("sH"), sHost.c_str(), uPort);
	if (!pyRes) {
		CString sRetMsg = m_pModPython->GetPyExceptionStr();
		DEBUG("python socket failed in OnAccepted: " << sRetMsg);
		Close();
	}
	int res = SWIG_ConvertPtr(pyRes, (void**)&result, SWIG_TypeQuery("CPySocket*"), 0);
	if (!SWIG_IsOK(res)) {
		DEBUG("python socket was expected to return new socket from OnAccepted, but error=" << res);
		Close();
		result = NULL;
	}
    if (!result) {
        DEBUG("modpython: OnAccepted didn't return new socket");
    }
	Py_CLEAR(pyRes);
	return result;
}
예제 #11
0
      Type asType(VALUE v)
      {
        Type *ptr = nullptr;

        int res = SWIG_ConvertPtr(v, reinterpret_cast<void **>(&ptr), getTypeInfo<Type *>(), 0);
        if (SWIG_IsOK(res))
        {
          if (!ptr)
          {
            throw std::runtime_error(std::string("Result is null, trying to get type: ") + typeid(Type).name());
          }

          if (SWIG_IsNewObj(res)) {
            Type obj = *ptr;
            delete ptr;
            return obj;
          } else {
            return *ptr;
          }
        } else {
          throw std::runtime_error(std::string("Unable to convert object to type: ") + typeid(Type).name());
        }
      }
SWIGINTERNINLINE int SWIG_CheckState(int r) { 
  return SWIG_IsOK(r) ? SWIG_CastRank(r) + 1 : 0; 
}
SWIGINTERNINLINE int SWIG_AddCast(int r) { 
  return SWIG_IsOK(r) ? ((SWIG_CastRank(r) < SWIG_MAXCASTRANK) ? (r + 1) : SWIG_ERROR) : r;
}
Teuchos::RCP< Epetra_MultiVector > *
convertPythonToEpetraMultiVector(PyObject * pyobj)
{
  // SWIG initialization
  static swig_type_info * swig_EMV_ptr =
    SWIG_TypeQuery("Teuchos::RCP< Epetra_MultiVector >*");
  static swig_type_info * swig_DMDV_ptr =
    SWIG_TypeQuery("Teuchos::RCP< Domi::MDVector<double> >*");
  //
  // Get the default communicator
  const Teuchos::RCP< const Teuchos::Comm<int> > comm =
    Teuchos::DefaultComm<int>::getComm();
  //
  // Result objects
  void *argp = 0;
  Teuchos::RCP< Epetra_MultiVector > * result = 0;
  Teuchos::RCP< Epetra_MultiVector > emv_rcp;
  Teuchos::RCP< Domi::MDVector<double> > dmdv_rcp;
  int newmem = 0;
  //
  // Check if the Python object is a wrapped Epetra_MultiVector
  int res = SWIG_ConvertPtrAndOwn(pyobj, &argp, swig_EMV_ptr, 0, &newmem);
  if (SWIG_IsOK(res))
  {
    result =
        reinterpret_cast< Teuchos::RCP< Epetra_MultiVector > * >(argp);
    return result;
  }

#ifdef HAVE_DOMI
  //
  // Check if the Python object is a wrapped Domi::MDVector<double>
  newmem = 0;
  res = SWIG_ConvertPtrAndOwn(pyobj, &argp, swig_DMDV_ptr, 0, &newmem);
  if (SWIG_IsOK(res))
  {
    if (newmem & SWIG_CAST_NEW_MEMORY)
    {
      dmdv_rcp =
        *reinterpret_cast< Teuchos::RCP< Domi::MDVector<double> > * >(argp);
      delete reinterpret_cast< Teuchos::RCP< Domi::MDVector<double> > * >(argp);
      //** result = &(dmdv_rcp->getEpetraMultiVectorView());
    }
    else
    {
      dmdv_rcp =
        *reinterpret_cast< Teuchos::RCP< Domi::MDVector<double> > * >(argp);
      //** result = &(dmdv_rcp->getEpetraMultiVectorView());
    }
    return result;
  }
  //
  // Check if the Python object supports the DistArray Protocol
  if (PyObject_HasAttrString(pyobj, "__distarray__"))
  {
    DistArrayProtocol dap(pyobj);
    dmdv_rcp = convertToMDVector<double>(comm, dap);
    //** result = &(dmdv_rcp->getEpetraMultiVectorView());
    return result;
  }
#endif

  //
  // Check if the environment is serial, and if so, check if the
  // Python object is a NumPy array
  if (comm->getSize() == 1)
  {
    if (PyArray_Check(pyobj))
    {
      //** result = new Teuchos::RCP< Epetra_NumPyMultiVector >(
      //**           new Epetra_NumPyMultiVector(pyobj));
      return result;
    }
  }
  //
  // If we get to this point, then none of our known converters will
  // work, so it is time to throw an exception.
  PyErr_Format(PyExc_TypeError, "Could not convert argument of type '%s' to "
               "an Epetra_MultiVector",
               PyString_AsString(PyObject_Str(PyObject_Type(pyobj))));
  throw PythonException();
}
예제 #15
0
int lua_multimin_minimize(lua_State * L) {
    bool ssdel=false;
    double eps=0.00001;
    double tol=0.0001;
    double step_size=0.01;
    int maxiter=1000;
    bool print=false;
    array<double> * x=0;
    array<double> * ss=0;
    const gsl_multimin_fminimizer_type *Tf = 0;
    const gsl_multimin_fdfminimizer_type *Tdf = 0;


    multi_param mp;
    mp.L=L;
    mp.fdf_index=-1;

    lua_pushstring(L,"f");
    lua_gettable(L,-2);
    if(lua_isfunction(L,-1)) {
        mp.f_index=luaL_ref(L, LUA_REGISTRYINDEX);
    } else {
        luaL_error(L,"%s\n","missing function");
    }

    lua_pushstring(L,"df");
    lua_gettable(L,-2);
    if(lua_isfunction(L,-1)) {
        mp.df_index=luaL_ref(L, LUA_REGISTRYINDEX);
        Tdf= gsl_multimin_fdfminimizer_conjugate_fr;
    } else {
        lua_pop(L,1);
        Tf= gsl_multimin_fminimizer_nmsimplex2;
    }

    lua_pushstring(L,"fdf");
    lua_gettable(L,-2);
    if(lua_isfunction(L,-1)) {
        mp.fdf_index=luaL_ref(L, LUA_REGISTRYINDEX);
    } else {
        lua_pop(L,1);
        mp.fdf_index=-1;
    }

    lua_pushstring(L,"algorithm");
    lua_gettable(L,-2);
    if(lua_isstring(L,-1)) {
        if(Tf!=0) {
            if(!strcmp(lua_tostring(L,-1),"nmsimplex")) {
                Tf = gsl_multimin_fminimizer_nmsimplex;
            } else if(!strcmp(lua_tostring(L,-1),"nmsimplex2rand")) {
                Tf = gsl_multimin_fminimizer_nmsimplex2rand;
            } else if(!strcmp(lua_tostring(L,-1),"nmsimplex2")) {
                Tf = gsl_multimin_fminimizer_nmsimplex2;
            } else {
                luaL_error(L,"%s\n","invalid algorithm");
            }
        } else {
            if(!strcmp(lua_tostring(L,-1),"conjugate_pr")) {
                Tdf = gsl_multimin_fdfminimizer_conjugate_pr;
            } else if(!strcmp(lua_tostring(L,-1),"steepest_descent")) {
                Tdf = gsl_multimin_fdfminimizer_steepest_descent;
            } else if(!strcmp(lua_tostring(L,-1),"vector_bfgs")) {
                Tdf = gsl_multimin_fdfminimizer_vector_bfgs;
            } else if(!strcmp(lua_tostring(L,-1),"vector_bfgs2")) {
                Tdf = gsl_multimin_fdfminimizer_vector_bfgs2;
            } else if(!strcmp(lua_tostring(L,-1),"conjugate_fr")) {
                Tdf = gsl_multimin_fdfminimizer_conjugate_fr;
            } else {
                luaL_error(L,"%s\n","invalid algorithm");
            }
        }
    }
    lua_pop(L,1);

    lua_pushstring(L,"show_iterations");
    lua_gettable(L,-2);
    if(lua_isboolean(L,-1)) {
        print=(lua_toboolean(L,-1)==1);
    }
    lua_pop(L,1);

    lua_pushstring(L,"eps");
    lua_gettable(L,-2);
    if(lua_isnumber(L,-1)) {
        eps=lua_tonumber(L,-1);
    }
    lua_pop(L,1);

    lua_pushstring(L,"step_size");
    lua_gettable(L,-2);
    if(lua_isnumber(L,-1)) {
        step_size=lua_tonumber(L,-1);
    }
    lua_pop(L,1);

    lua_pushstring(L,"tol");
    lua_gettable(L,-2);
    if(lua_isnumber(L,-1)) {
        tol=lua_tonumber(L,-1);
    }
    lua_pop(L,1);

    lua_pushstring(L,"maxiter");
    lua_gettable(L,-2);
    if(lua_isnumber(L,-1)) {
        maxiter=(int)lua_tonumber(L,-1);
    }
    lua_pop(L,1);

    lua_pushstring(L,"starting_point");
    lua_gettable(L,-2);
    if(!lua_isuserdata(L,-1)) lua_error(L);
    if (!SWIG_IsOK(SWIG_ConvertPtr(L,-1,(void**)&x,SWIGTYPE_p_arrayT_double_t,0))){
        luaL_error(L,"%s\n","missing starting point");
    }
    lua_pop(L,1);

    if(Tf) {
        lua_pushstring(L,"step_sizes");
        lua_gettable(L,-2);
        if(lua_isuserdata(L,-1)) {
            if (!SWIG_IsOK(SWIG_ConvertPtr(L,-1,(void**)&ss,SWIGTYPE_p_arrayT_double_t,0))){
                lua_error(L);
            }
        } else {
            ssdel=true;
            ss=new array<double>(x->size());
            ss->set_all(1.0);
            if(lua_isnumber(L,-1)) {
                double v=lua_tonumber(L,-1);
                ss->set_all(v);
            }
        }
        lua_pop(L,1);
    }

    lua_pop(L,1);

    if(Tf) {
        gsl_multimin_fminimizer *s = NULL;
        gsl_vector SS, X;
        gsl_multimin_function minex_func;

        int iter = 0;
        int status;
        double size;
        int N=x->size();

        /* Starting point */
        X.size=x->size();
        X.stride=1;
        X.data=x->data();
        X.owner=0;

        /* Set initial step sizes */
        SS.size=ss->size();
        SS.stride=1;
        SS.data=ss->data();
        SS.owner=0;

        /* Initialize method and iterate */
        minex_func.n = N;
        minex_func.f = multimin_f_cb;
        minex_func.params = &mp;

        s = gsl_multimin_fminimizer_alloc (Tf, N);
        gsl_multimin_fminimizer_set (s, &minex_func, &X, &SS);
        if(print)  printf ("running algorithm '%s'\n",
                gsl_multimin_fminimizer_name (s));
        do
        {
            iter++;
            status = gsl_multimin_fminimizer_iterate(s);

            if (status)
                break;

            size = gsl_multimin_fminimizer_size (s);
            status = gsl_multimin_test_size (size, eps);

            if (status == GSL_SUCCESS)
            {
                if(print) printf ("converged to minimum at\n");
            }

            if(print) printf ("%5d f() = %12.3f size = %.9f\n",
                    iter,
                    s->fval, size);
        } while (status == GSL_CONTINUE && iter < maxiter);
        for(int i=0;i<N;++i) x->set(i,gsl_vector_get(s->x,i));
        luaL_unref(L, LUA_REGISTRYINDEX, mp.f_index);
        gsl_multimin_fminimizer_free (s);
    } else {
        gsl_multimin_fdfminimizer *s = NULL;
        gsl_vector X;
        gsl_multimin_function_fdf minex_func;

        int iter = 0;
        int status;
        double size;
        int N=x->size();

        /* Starting point */
        X.size=x->size();
        X.stride=1;
        X.data=x->data();
        X.owner=0;

        /* Initialize method and iterate */
        minex_func.n = N;
        minex_func.f = multimin_f_cb;
        minex_func.df = multimin_df_cb;
        minex_func.fdf = multimin_fdf_cb;
        minex_func.params = &mp;

        s = gsl_multimin_fdfminimizer_alloc (Tdf, N);
        gsl_multimin_fdfminimizer_set (s, &minex_func, &X, step_size, tol);
        if(print)  printf ("running algorithm '%s'\n",
                gsl_multimin_fdfminimizer_name (s));
        do
        {
            iter++;
            status = gsl_multimin_fdfminimizer_iterate(s);

            if (status)
                break;

            status = gsl_multimin_test_gradient (s->gradient, eps);

            if (status == GSL_SUCCESS)
            {
                if(print) printf ("converged to minimum at\n");
            }

            if(print) printf ("%5d f() = %12.3f\n",
                    iter,
                    s->f);
        } while (status == GSL_CONTINUE && iter < maxiter);
        for(int i=0;i<N;++i) x->set(i,gsl_vector_get(s->x,i));
        luaL_unref(L, LUA_REGISTRYINDEX, mp.f_index);
        luaL_unref(L, LUA_REGISTRYINDEX, mp.df_index);
        gsl_multimin_fdfminimizer_free (s);
    }
    if(mp.fdf_index>=0) {
        luaL_unref(L, LUA_REGISTRYINDEX, mp.fdf_index);
    }
    if(ssdel) {
        delete ss;
    }
    return 0;
}
예제 #16
0
int lua_multiroot_solve(lua_State * L) {
    double eps=0.00001;
    int maxiter=1000;
    bool print=false;
    array<double> * x=0;
    const gsl_multiroot_fsolver_type *Tf = 0;
    const gsl_multiroot_fdfsolver_type *Tdf = 0;

    multi_param mp;
    mp.L=L;
    mp.fdf_index=-1;

    lua_pushstring(L,"f");
    lua_gettable(L,-2);
    if(lua_isfunction(L,-1)) {
        mp.f_index=luaL_ref(L, LUA_REGISTRYINDEX);
    } else {
        luaL_error(L,"%s\n","missing function");
    }

    lua_pushstring(L,"df");
    lua_gettable(L,-2);
    if(lua_isfunction(L,-1)) {
        mp.df_index=luaL_ref(L, LUA_REGISTRYINDEX);
        Tdf= gsl_multiroot_fdfsolver_hybridsj;
    } else {
        lua_pop(L,1);
        Tf=  gsl_multiroot_fsolver_hybrids;
    }

    lua_pushstring(L,"fdf");
    lua_gettable(L,-2);
    if(lua_isfunction(L,-1)) {
        mp.fdf_index=luaL_ref(L, LUA_REGISTRYINDEX);
    } else {
        lua_pop(L,1);
        mp.fdf_index=-1;
    }

    lua_pushstring(L,"algorithm");
    lua_gettable(L,-2);
    if(lua_isstring(L,-1)) {
        if(Tf) {
            if(!strcmp(lua_tostring(L,-1),"hybrid")) {
                Tf = gsl_multiroot_fsolver_hybrid;
            } else if(!strcmp(lua_tostring(L,-1),"dnewton")) {
                Tf = gsl_multiroot_fsolver_dnewton;
            } else if(!strcmp(lua_tostring(L,-1),"hybrids")) {
                Tf = gsl_multiroot_fsolver_hybrids;
            } else if(!strcmp(lua_tostring(L,-1),"broyden")) {
                Tf = gsl_multiroot_fsolver_broyden;
            } else {
                luaL_error(L,"%s\n","invalid algorithm");
            }
        } else {
            if(!strcmp(lua_tostring(L,-1),"hybridj")) {
                Tdf = gsl_multiroot_fdfsolver_hybridj;
            } else if(!strcmp(lua_tostring(L,-1),"newton")) {
                Tdf = gsl_multiroot_fdfsolver_newton;
            } else if(!strcmp(lua_tostring(L,-1),"hybridsj")) {
                Tdf = gsl_multiroot_fdfsolver_hybridsj;
            } else if(!strcmp(lua_tostring(L,-1),"gnewton")) {
                Tdf = gsl_multiroot_fdfsolver_gnewton;
            } else {
                luaL_error(L,"%s\n","invalid algorithm");
            }
        }
    }
    lua_pop(L,1);

    lua_pushstring(L,"show_iterations");
    lua_gettable(L,-2);
    if(lua_isboolean(L,-1)) {
        print=(lua_toboolean(L,-1)==1);
    }
    lua_pop(L,1);

    lua_pushstring(L,"eps");
    lua_gettable(L,-2);
    if(lua_isnumber(L,-1)) {
        eps=lua_tonumber(L,-1);
    }
    lua_pop(L,1);

    lua_pushstring(L,"maxiter");
    lua_gettable(L,-2);
    if(lua_isnumber(L,-1)) {
        maxiter=(int)lua_tonumber(L,-1);
    }
    lua_pop(L,1);

    lua_pushstring(L,"starting_point");
    lua_gettable(L,-2);
    if(!lua_isuserdata(L,-1)) lua_error(L);
    if (!SWIG_IsOK(SWIG_ConvertPtr(L,-1,(void**)&x,SWIGTYPE_p_arrayT_double_t,0))){
        lua_error(L);
    }
    lua_pop(L,1);

    lua_pop(L,1);
    if(Tf) {
        gsl_multiroot_fsolver *s = NULL;
        gsl_vector X;
        gsl_multiroot_function sol_func;

        int iter = 0;
        int status;
        double size;
        int N=x->size();

        /* Starting point */
        X.size=x->size();
        X.stride=1;
        X.data=x->data();
        X.owner=0;

        /* Initialize method and iterate */
        sol_func.n = N;
        sol_func.f = multiroot_f_cb;
        sol_func.params = &mp;

        s = gsl_multiroot_fsolver_alloc (Tf, N);
        gsl_multiroot_fsolver_set (s, &sol_func, &X);
        if(print)  printf ("running algorithm '%s'\n",
                gsl_multiroot_fsolver_name (s));
        do
        {
            iter++;
            status = gsl_multiroot_fsolver_iterate(s);

            if (status)
                break;

            status = gsl_multiroot_test_residual (s->f, eps);

            if(print) {
                printf ("%5d f() = ", iter);
                gsl_vector_fprintf(stdout,  s->f, "%f");
            }
        } while (status == GSL_CONTINUE && iter < maxiter);
        for(int i=0;i<N;++i) x->set(i,gsl_vector_get(s->x,i));
        luaL_unref(L, LUA_REGISTRYINDEX, mp.f_index);
        gsl_multiroot_fsolver_free (s);
    } else {
        gsl_multiroot_fdfsolver *s = NULL;
        gsl_vector X;
        gsl_multiroot_function_fdf sol_func;

        int iter = 0;
        int status;
        double size;
        int N=x->size();

        /* Starting point */
        X.size=x->size();
        X.stride=1;
        X.data=x->data();
        X.owner=0;

        /* Initialize method and iterate */
        sol_func.n = N;
        sol_func.f = multiroot_f_cb;
        sol_func.df = multiroot_df_cb;
        sol_func.fdf = multiroot_fdf_cb;
        sol_func.params = &mp;

        s = gsl_multiroot_fdfsolver_alloc (Tdf, N);
        gsl_multiroot_fdfsolver_set (s, &sol_func, &X);
        if(print)  printf ("running algorithm '%s'\n",
                gsl_multiroot_fdfsolver_name (s));
        do
        {
            iter++;
            status = gsl_multiroot_fdfsolver_iterate(s);

            if (status)
                break;

            status = gsl_multiroot_test_residual (s->f, eps);

            if(print) {
                printf ("%5d f() = ", iter);
                gsl_vector_fprintf(stdout,  s->f, "%f");
            }
        } while (status == GSL_CONTINUE && iter < maxiter);
        for(int i=0;i<N;++i) x->set(i,gsl_vector_get(s->x,i));
        luaL_unref(L, LUA_REGISTRYINDEX, mp.f_index);
        luaL_unref(L, LUA_REGISTRYINDEX, mp.df_index);
        gsl_multiroot_fdfsolver_free (s);
    }
    if(mp.fdf_index>=0) luaL_unref(L, LUA_REGISTRYINDEX, mp.fdf_index);
    return 0;
}