Ejemplo n.º 1
0
ASFUNCTIONBODY(Array,forEach)
{
	assert_and_throw(argslen == 1 || argslen == 2);
	Array* th=static_cast<Array*>(obj);
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];

	std::map<uint32_t, data_slot>::iterator it=th->data.begin();
	for(;it != th->data.end();++it)
	{
		assert_and_throw(it->second.type==DATA_OBJECT);
		params[0] = it->second.data;
		it->second.data->incRef();
		params[1] = abstract_i(it->first);
		params[2] = th;
		th->incRef();

		ASObject *funcret;
		if( argslen == 1 )
		{
			funcret=f->call(getSys()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcret=f->call(args[1], params, 3);
		}
		if(funcret)
			funcret->decRef();
	}

	return NULL;
}
Ejemplo n.º 2
0
ASFUNCTIONBODY(Array,forEach)
{
	assert_and_throw(argslen == 1 || argslen == 2);
	Array* th=static_cast<Array*>(obj);
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];

	for(unsigned int i=0; i < th->data.size(); i++)
	{
		assert_and_throw(th->data[i].type==DATA_OBJECT);
		params[0] = th->data[i].data;
		th->data[i].data->incRef();
		params[1] = abstract_i(i);
		params[2] = th;
		th->incRef();

		ASObject *funcret;
		if( argslen == 1 )
		{
			funcret=f->call(new Null, params, 3);
		}
		else
		{
			args[1]->incRef();
			funcret=f->call(args[1], params, 3);
		}
		if(funcret)
			funcret->decRef();
	}

	return NULL;
}
Ejemplo n.º 3
0
static void buildin_setfenv(const vector<LuaValue>& args, vector<LuaValue>& rets) {
    IFunction *func = NULL;
    if (args[0].isTypeOf(LVT_Number)) {
        auto n = (int)args[0].getNumber();
        ASSERT(n >= 0);
        if (n > 0) func = Runtime::instance()->getFrame(-n - 1)->getFunc();
    } else func = args[0].getFunction();

    if (func == NULL) {
        Runtime::instance()->setGlobalTable(args[1].getTable());
    } else {
        func->setfenv(args[1].getTable());
    }
}
Ejemplo n.º 4
0
Point inputDirection(const IFunction &f, bool assume1 = false)
{
    if (f.dimensions() > 1 && !assume1) {
        cout << "-- Input direction --" << endl;
        return PointHelper::inputPoint(f.dimensions());
    }
    else {
        vector<double> start = vector<double>(f.dimensions());
        for (vector<double>::iterator i = start.begin(); i != start.end(); ++i) {
            (*i) = 1;
        }
        return Point(start);
    }
}
Ejemplo n.º 5
0
static void buildin_getfenv(const vector<LuaValue>& args, vector<LuaValue>& rets) {
    IFunction *func = NULL;
    if (args.empty()) func = Runtime::instance()->getFrame(-2)->getFunc();
    else if (args[0].isTypeOf(LVT_Number)) {
        auto n = (int)args[0].getNumber();
        ASSERT(n >= 0);
        if (n > 0) func = Runtime::instance()->getFrame(-n - 1)->getFunc();
    } else func = args[0].getFunction();

    LuaTable* table = NULL;
    if (func == NULL) table = Runtime::instance()->getGlobalTable();
    else table = func->getfenv();
    table->addRef();
    rets.push_back(LuaValue(table));
}
/**
* Add a column to a table workspace filled with double values
* calculated with a function wich uses another numeric column as its x values.
* @param tws :: The table workspace.
* @param xColumn :: Name of the column with the x-values. Must exist and be numeric
* @param yColumn :: Name of a column to store the calculated values. If a column with
*  this name exists it will be overwritten if numeric or exception thrown otherwise.
*  If the column doesn't exist it will be created.
* @param fun :: A function to calculate the values.
*/
void AddFunctionValuesToTable::addColumn(API::TableWorkspace_ptr tws,
                                         const std::string& xColumn,
                                         const std::string& yColumn,
                                         const IFunction& fun,
                                         API::NumericColumn::PlotRole role
                                         )
{
    auto& x = tws->getDoubleData( xColumn );
    if ( tws->hasColumn( yColumn ) )
    {
        if ( !tws->getColumn( yColumn )->isNumeric() )
            throw std::runtime_error("Column "+yColumn+" isn't numeric");
    }
    else
    {
        tws->addColumn( "double", yColumn );
    }
    tws->getColumn( yColumn )->asNumeric()->setPlotRole(role);
    auto& y = tws->getDoubleData( yColumn );

    FunctionDomain1DView domain( x.data(), x.size() );
    FunctionValues values( domain );
    fun.function( domain, values );
    for(size_t i = 0; i < y.size(); ++i)
    {
        y[i] = values.getCalculated(i);
    }
}
Ejemplo n.º 7
0
ASFUNCTIONBODY(Vector, every)
{
	Vector* th=static_cast<Vector*>(obj);
	if (argslen < 1)
		throwError<ArgumentError>(kWrongArgumentCountError, "Vector.some", "1", Integer::toString(argslen));
	if (!args[0]->is<IFunction>())
		throwError<TypeError>(kCheckTypeFailedError, args[0]->getClassName(), "Function");
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];
	ASObject *funcRet;

	for(unsigned int i=0; i < th->size(); i++)
	{
		if (th->vec[i])
		{
			params[0] = th->vec[i];
			th->vec[i]->incRef();
		}
		else
			params[0] = obj->getSystemState()->getNullRef();
		params[1] = abstract_i(obj->getSystemState(),i);
		params[2] = th;
		th->incRef();

		if(argslen==1)
		{
			funcRet=f->call(obj->getSystemState()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcRet=f->call(args[1], params, 3);
		}
		if(funcRet)
		{
			if (funcRet->is<Undefined>() || funcRet->is<Null>())
				throwError<TypeError>(kCallOfNonFunctionError, funcRet->toString());
			if(!Boolean_concrete(funcRet))
			{
				return funcRet;
			}
			funcRet->decRef();
		}
	}
	return abstract_b(obj->getSystemState(),true);
}
Ejemplo n.º 8
0
ASFUNCTIONBODY(Vector, every)
{
	Vector* th=static_cast<Vector*>(obj);
	if (argslen < 1)
		throw Class<ArgumentError>::getInstanceS("Error #1063");
	if (!args[0]->is<IFunction>())
		throw Class<TypeError>::getInstanceS("Error #1034"); 
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];
	ASObject *funcRet;

	for(unsigned int i=0; i < th->size(); i++)
	{
		if (th->vec[i])
		{
			params[0] = th->vec[i];
			th->vec[i]->incRef();
		}
		else
			params[0] = getSys()->getNullRef();
		params[1] = abstract_i(i);
		params[2] = th;
		th->incRef();

		if(argslen==1)
		{
			funcRet=f->call(getSys()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcRet=f->call(args[1], params, 3);
		}
		if(funcRet)
		{
			if (funcRet->is<Undefined>() || funcRet->is<Null>())
				throw Class<TypeError>::getInstanceS("Error #1006");
			if(!Boolean_concrete(funcRet))
			{
				return funcRet;
			}
			funcRet->decRef();
		}
	}
	return abstract_b(true);
}
Ejemplo n.º 9
0
/**
 * Reset the reference
 * @param fun :: Pointer to a function (composite or simple).
 * @param index :: Index of a parameter of fun
 * @param isDefault :: Flag to mark as default the value of an object associated with this reference:
 *  a tie or a constraint. 
 */
void ParameterReference::reset(IFunction* fun, std::size_t index, bool isDefault)
{
  IFunction* fLocal = fun;
  size_t iLocal = index;
  CompositeFunction* cf = dynamic_cast<CompositeFunction*>(fun);
  while (cf)
  {
    size_t iFun = cf->functionIndex(iLocal); // TODO squashing the warning breaks the code
    fLocal = cf->getFunction(iFun).get();
    iLocal = fLocal->parameterIndex(cf->parameterLocalName(iLocal));
    cf = dynamic_cast<CompositeFunction*>(fLocal);
  }

  m_function = fLocal;
  m_index = iLocal;
  m_isDefault = isDefault;
}
Ejemplo n.º 10
0
int main(int argc, const char * argv[])
{
    IFunction *targetFunction = selectFunction();   // Выбор целевой функции
    
    Point startPoint = inputPoint(*targetFunction);   // Ввод стартовой точки
    Point direction = inputDirection(*targetFunction, true); // Направление принимаем за единичный вектор
    
    double precision = inputPrecision();    // Ввод точности
    
    Point min = Newton().optimize(*targetFunction, startPoint, direction, precision);
    
    cout << "Minimum is:\t";
    min.print(precision);
    cout << endl << "F(min) is:\t";
    cout << targetFunction->value(min);
    
    return 0;
}
Ejemplo n.º 11
0
ASFUNCTIONBODY(EventDispatcher,dispatchEvent)
{
	EventDispatcher* th=Class<EventDispatcher>::cast(obj);
	if(args[0]->getClass()==NULL || !(args[0]->getClass()->isSubClass(Class<Event>::getClass())))
		return abstract_b(false);

	args[0]->incRef();
	_R<Event> e=_MR(Class<Event>::cast(args[0]));
	assert_and_throw(e->type!="");
	if(!e->target.isNull())
	{
		//Object must be cloned, closing is implemented with the clone AS method
		multiname cloneName;
		cloneName.name_type=multiname::NAME_STRING;
		cloneName.name_s="clone";
		cloneName.ns.push_back(nsNameAndKind("",PACKAGE_NAMESPACE));

		_NR<ASObject> clone=e->getVariableByMultiname(cloneName);
		if(!clone.isNull() && clone->getObjectType()==T_FUNCTION)
		{
			IFunction* f = static_cast<IFunction*>(clone.getPtr());
			e->incRef();
			ASObject* funcRet=f->call(e.getPtr(),NULL,0);
			//Verify that the returned object is actually an event
			Event* newEvent=dynamic_cast<Event*>(funcRet);
			if(newEvent==NULL)
			{
				if(funcRet)
					funcRet->decRef();
				return abstract_b(false);
			}
			e=_MR(newEvent);
		}
		else
		{
			//TODO: support cloning of actual type
			LOG(LOG_NOT_IMPLEMENTED,"Event cloning not supported!");
			e=_MR(Class<Event>::getInstanceS(e->type,e->bubbles, e->cancelable));
		}
	}
	th->incRef();
	ABCVm::publicHandleEvent(_MR(th), e);
	return abstract_b(true);
}
Ejemplo n.º 12
0
ASFUNCTIONBODY(Vector,filter)
{
	if (argslen < 1 || argslen > 2)
		throw Class<ArgumentError>::getInstanceS("Error #1063"); 
	if (!args[0]->is<IFunction>())
		throw Class<TypeError>::getInstanceS("Error #1034"); 
	Vector* th=static_cast<Vector*>(obj);
	  
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];
	Vector* ret= (Vector*)obj->getClass()->getInstance(true,NULL,0);
	ASObject *funcRet;

	for(unsigned int i=0;i<th->size();i++)
	{
		if (!th->vec[i])
			continue;
		params[0] = th->vec[i];
		th->vec[i]->incRef();
		params[1] = abstract_i(i);
		params[2] = th;
		th->incRef();

		if(argslen==1)
		{
			funcRet=f->call(getSys()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcRet=f->call(args[1], params, 3);
		}
		if(funcRet)
		{
			if(Boolean_concrete(funcRet))
			{
				th->vec[i]->incRef();
				ret->vec.push_back(th->vec[i]);
			}
			funcRet->decRef();
		}
	}
	return ret;
}
Ejemplo n.º 13
0
ASFUNCTIONBODY(Vector,filter)
{
	if (argslen < 1 || argslen > 2)
		throwError<ArgumentError>(kWrongArgumentCountError, "Vector.filter", "1", Integer::toString(argslen));
	if (!args[0]->is<IFunction>())
		throwError<TypeError>(kCheckTypeFailedError, args[0]->getClassName(), "Function");
	Vector* th=static_cast<Vector*>(obj);
	  
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];
	Vector* ret= (Vector*)obj->getClass()->getInstance(true,NULL,0);
	ASObject *funcRet;

	for(unsigned int i=0;i<th->size();i++)
	{
		if (!th->vec[i])
			continue;
		params[0] = th->vec[i];
		th->vec[i]->incRef();
		params[1] = abstract_i(obj->getSystemState(),i);
		params[2] = th;
		th->incRef();

		if(argslen==1)
		{
			funcRet=f->call(obj->getSystemState()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcRet=f->call(args[1], params, 3);
		}
		if(funcRet)
		{
			if(Boolean_concrete(funcRet))
			{
				th->vec[i]->incRef();
				ret->vec.push_back(th->vec[i]);
			}
			funcRet->decRef();
		}
	}
	return ret;
}
Ejemplo n.º 14
0
/// Execute
void EstimatePeakErrors::exec() {

  IFunction_sptr function = getProperty("Function");

  ITableWorkspace_sptr results =
      WorkspaceFactory::Instance().createTable("TableWorkspace");
  results->addColumn("str", "Parameter");
  results->addColumn("double", "Value");
  results->addColumn("double", "Error");

  auto matrix = function->getCovarianceMatrix();
  if (!matrix) {
    g_log.warning() << "Function doesn't have covariance matrix.\n";
    setProperty("OutputWorkspace", results);
    return;
  }

  IPeakFunction *peak = dynamic_cast<IPeakFunction *>(function.get());

  if (peak) {
    GSLMatrix covariance(*matrix);
    calculatePeakValues(*peak, *results, covariance, "");
  } else {
    CompositeFunction *cf = dynamic_cast<CompositeFunction *>(function.get());
    if (cf) {
      size_t ip = 0;
      for (size_t i = 0; i < cf->nFunctions(); ++i) {
        IFunction *fun = cf->getFunction(i).get();
        size_t np = fun->nParams();
        peak = dynamic_cast<IPeakFunction *>(fun);
        if (peak) {
          std::string prefix = "f" + std::to_string(i) + ".";
          GSLMatrix covariance(*matrix, ip, ip, np, np);
          calculatePeakValues(*peak, *results, covariance, prefix);
        }
        ip += np;
      }
    } else {
      g_log.warning() << "Function has no peaks.\n";
    }
  }

  setProperty("OutputWorkspace", results);
}
Ejemplo n.º 15
0
ASFUNCTIONBODY(Vector, some)
{
	if (argslen < 1)
		throw Class<ArgumentError>::getInstanceS("Error #1063");
	if (!args[0]->is<IFunction>())
		throw Class<TypeError>::getInstanceS("Error #1034"); 
	Vector* th=static_cast<Vector*>(obj);
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];
	ASObject *funcRet;

	for(unsigned int i=0; i < th->size(); i++)
	{
		if (!th->vec[i])
			continue;
		params[0] = th->vec[i];
		th->vec[i]->incRef();
		params[1] = abstract_i(i);
		params[2] = th;
		th->incRef();

		if(argslen==1)
		{
			funcRet=f->call(new Null, params, 3);
		}
		else
		{
			args[1]->incRef();
			funcRet=f->call(args[1], params, 3);
		}
		if(funcRet)
		{
			if(Boolean_concrete(funcRet))
			{
				return funcRet;
			}
			funcRet->decRef();
		}
	}
	return abstract_b(false);
}
Ejemplo n.º 16
0
ASFUNCTIONBODY(Array,filter)
{
	Array* th=static_cast<Array*>(obj);
	assert_and_throw(argslen==1 || argslen==2);
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];
	Array* ret=Class<Array>::getInstanceS();
	ASObject *funcRet;

	std::map<uint32_t, data_slot>::iterator it=th->data.begin();
	for(;it != th->data.end();++it)
	{
		assert_and_throw(it->second.type==DATA_OBJECT);
		params[0] = it->second.data;
		it->second.data->incRef();
		params[1] = abstract_i(it->first);
		params[2] = th;
		th->incRef();

		if(argslen==1)
		{
			funcRet=f->call(getSys()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcRet=f->call(args[1], params, 3);
		}
		if(funcRet)
		{
			if(Boolean_concrete(funcRet))
			{
				it->second.data->incRef();
				ret->push(_MR(it->second.data));
			}
			funcRet->decRef();
		}
	}
	return ret;
}
Ejemplo n.º 17
0
ASFUNCTIONBODY(Array,filter)
{
	Array* th=static_cast<Array*>(obj);
	assert_and_throw(argslen==1 || argslen==2);
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];
	Array* ret=Class<Array>::getInstanceS();
	ASObject *funcRet;

	for(unsigned int i=0;i<th->data.size();i++)
	{
		assert_and_throw(th->data[i].type==DATA_OBJECT);
		params[0] = th->data[i].data;
		th->data[i].data->incRef();
		params[1] = abstract_i(i);
		params[2] = th;
		th->incRef();

		if(argslen==1)
		{
			funcRet=f->call(new Null, params, 3);
		}
		else
		{
			args[1]->incRef();
			funcRet=f->call(args[1], params, 3);
		}
		if(funcRet)
		{
			if(Boolean_concrete(funcRet))
			{
				th->data[i].data->incRef();
				ret->push(th->data[i].data);
			}
			funcRet->decRef();
		}
	}
	return ret;
}
Ejemplo n.º 18
0
ASFUNCTIONBODY(EventDispatcher,dispatchEvent)
{
	EventDispatcher* th=Class<EventDispatcher>::cast(obj);
	if(args[0]->getClass()==NULL || !(args[0]->getClass()->isSubClass(Class<Event>::getClass())))
		return abstract_b(false);

	args[0]->incRef();
	_R<Event> e=_MR(Class<Event>::cast(args[0]));
	assert_and_throw(e->type!="");
	if(!e->target.isNull())
	{
		//Object must be cloned, closing is implemented with the clone AS method
		multiname cloneName(NULL);
		cloneName.name_type=multiname::NAME_STRING;
		cloneName.name_s_id=getSys()->getUniqueStringId("clone");
		cloneName.ns.push_back(nsNameAndKind("",NAMESPACE));

		_NR<ASObject> clone=e->getVariableByMultiname(cloneName);
		//Clone always exists since it's implemented in Event itself
		assert(!clone.isNull());
		IFunction* f = static_cast<IFunction*>(clone.getPtr());
		e->incRef();
		ASObject* funcRet=f->call(e.getPtr(),NULL,0);
		//Verify that the returned object is actually an event
		Event* newEvent=dynamic_cast<Event*>(funcRet);
		if(newEvent==NULL)
		{
			if(funcRet)
				funcRet->decRef();
			return abstract_b(false);
		}
		e=_MR(newEvent);
	}
	if(!th->forcedTarget.isNull())
		e->setTarget(th->forcedTarget);
	th->incRef();
	ABCVm::publicHandleEvent(_MR(th), e);
	return abstract_b(true);
}
Ejemplo n.º 19
0
ASFUNCTIONBODY(Vector,forEach)
{
	if (argslen < 1)
		throw Class<ArgumentError>::getInstanceS("Error #1063");
	if (!args[0]->is<IFunction>())
		throw Class<TypeError>::getInstanceS("Error #1034"); 
	Vector* th=static_cast<Vector*>(obj);
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];

	for(unsigned int i=0; i < th->size(); i++)
	{
		if (!th->vec[i])
			continue;
		params[0] = th->vec[i];
		th->vec[i]->incRef();
		params[1] = abstract_i(i);
		params[2] = th;
		th->incRef();

		ASObject *funcret;
		if( argslen == 1 )
		{
			funcret=f->call(getSys()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcret=f->call(args[1], params, 3);
		}
		if(funcret)
			funcret->decRef();
	}

	return NULL;
}
Ejemplo n.º 20
0
ASFUNCTIONBODY(Vector,forEach)
{
	if (argslen < 1)
		throwError<ArgumentError>(kWrongArgumentCountError, "Vector.forEach", "1", Integer::toString(argslen));
	if (!args[0]->is<IFunction>())
		throwError<TypeError>(kCheckTypeFailedError, args[0]->getClassName(), "Function");
	Vector* th=static_cast<Vector*>(obj);
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];

	for(unsigned int i=0; i < th->size(); i++)
	{
		if (!th->vec[i])
			continue;
		params[0] = th->vec[i];
		th->vec[i]->incRef();
		params[1] = abstract_i(obj->getSystemState(),i);
		params[2] = th;
		th->incRef();

		ASObject *funcret;
		if( argslen == 1 )
		{
			funcret=f->call(obj->getSystemState()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcret=f->call(args[1], params, 3);
		}
		if(funcret)
			funcret->decRef();
	}

	return NULL;
}
Ejemplo n.º 21
0
Point inputPoint(const IFunction &f)
{
    cout << "-- Input start point --" << endl;
    return PointHelper::inputPoint(f.dimensions());
}