Example #1
0
Maybe<Real> NewtonPolySolve::FindSmallestPositiveRoot(const RPolynomial& p, const Real /*lastSmallestPositive*/) {
	if (NumberOfSignChanges(p) == 0)
		return Maybe<Real>();

	const CPolynomial q(p);
	//{
	//	bool converged;
	//	RPolynomial t = RPolynomial::MakeT();
	//	Scalar x = NextRoot(q, Scalar(lastSmallestPositive), &converged);
	//	if (converged && IsReal(x) && x.real() > 0 && NumberOfSignChanges(p.EvaluateAt(t + x.real() + .1)) == 0)
	//		return x.real();

	//	/*if (converged && )
	//		return x;*/
	//}

	
	const ScalarList roots = FindRoots(q);
	Maybe<Real> result;
	for (int i = 0 ; i < (int)roots.size() ; i++) {
		if (IsReal(roots[i]) && roots[i].real() > 0) {
			if (result.IsValid()) {
				result = std::min(result.Get(), roots[i].real());
			} else {
				result = roots[i].real();
			}
		}
	}
	return result;
}
    //---------------------------------------------------------------------------------
    void SymbolTable::evaluateFunction( AST::ConstantExpression& result, const String& name, const ScalarList& evaluatedArgs )
    {
        String funcname = name;

        const FunctionInfo* fi = getFunction( funcname );

        if ( !fi )
        {
            // note: getFunction( funcname ); calls errorHandler
            return;
        }

        if ( fi->argc >= -1 )    // -1 means any parameter count
        {

            if ( fi->argc == 0 && evaluatedArgs.size() != 0 )
            {
                //@todo BadFunctionCallException
                if ( mErrorHandler )
                {
                    Error err( Error::ERR_INVALIDPARAMS, "Function " + funcname + "() does not take any parameter." );
                    mErrorHandler->handleError( &err );
                }
            }

            else if ( fi->argc == 1 && evaluatedArgs.size() != 1 )
            {
                //@todo BadFunctionCallException
                if ( mErrorHandler )
                {
                    Error err( Error::ERR_INVALIDPARAMS, "Function " + funcname + "() takes exactly one parameter." );
                    mErrorHandler->handleError( &err );
                }
            }

            else if ( fi->argc == -1 )
            {
                // ok
            }

            else if ( static_cast<unsigned int>( fi->argc ) != evaluatedArgs.size() )
            {
                std::ostringstream oss;
                oss << "Function " << funcname << "() takes exactly " << fi->argc << " parameters.";
                //@todo BadFunctionCallException
                if ( mErrorHandler )
                {
                    Error err( Error::ERR_INVALIDPARAMS, oss.str() );
                    mErrorHandler->handleError( &err );
                }
            }

            fi->func( result, evaluatedArgs, mErrorHandler );
            return ;
        }


        //@todo UnknownSymbolException(std::string("Unknown function ") + funcname + "()"));
        if ( mErrorHandler )
        {
            Error err( Error::ERR_INVALIDPARAMS, "Unknown function " + funcname + "()" );
            mErrorHandler->handleError( &err );
        }
    }