Example #1
0
types::Function::ReturnValue sci_strcmp(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::Double* pOutDouble   = NULL;
    types::String* pString1     = NULL;
    types::String* pString2     = NULL;
    wchar_t* pwcChar3           = NULL;
    BOOL do_stricmp             = FALSE;

    if (in.size() < 2 || in.size() > 3)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "strcmp", 2, 3);
        return types::Function::Error;
    }
    if (_iRetCount != 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "strcmp", 1);
        return types::Function::Error;
    }
    if (in[0]->isString() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "strcmp", 1);
        return types::Function::Error;
    }
    if (in[1]->isString() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "strcmp", 2);
        return types::Function::Error;
    }

    pString1 = in[0]->getAs<types::String>();
    pString2 = in[1]->getAs<types::String>();



    if (pString1->getSize() != pString2->getSize() && pString2->isScalar() == false)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d.\n"), "strcmp", 2);
        return types::Function::Error;
    }

    if (in.size() == 3)
    {
        if (in[2]->isString() == false || in[2]->getAs<types::String>()->isScalar() == false || wcslen(in[2]->getAs<types::String>()->get(0)) != 1)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: Char expected.\n"), "strcmp", 3);
            return types::Function::Error;
        }

        pwcChar3 = in[2]->getAs<types::String>()->get(0);
        if ( (pwcChar3[0] != CHAR_I) && (pwcChar3[0] != CHAR_S))
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: %s or %s expected.\n"), "strcmp", 3, "'i' (stricmp)", "'s' (strcmp)");
            return types::Function::Error;
        }

        if (pwcChar3[0] == CHAR_I)
        {
            do_stricmp = TRUE;
        }
    }

    int *values = stringsCompare(pString1->get(), pString1->getSize(), pString2->get(), pString2->getSize(), do_stricmp);

    if (values)
    {
        pOutDouble  = new types::Double(pString1->getDims(), pString1->getDimsArray());
        pOutDouble->setInt(values);
        FREE(values);
    }
    else
    {
        Scierror(999, _("%s : No more memory.\n"), "strcmp");
    }


    out.push_back(pOutDouble);
    return types::Function::OK;
}
Example #2
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_triu(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    int iOffset = 0;

    if (in.size() < 1 || in.size() > 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "triu", 1, 2);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "triu", 1);
        return types::Function::Error;
    }


    if (in[0]->isGenericType() == false)
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_triu";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    if (in[0]->getAs<types::GenericType>()->getDims() > 2)
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_triu";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    if (in.size() == 2)
    {
        if (in[1]->isDouble() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), "triu", 2);
            return types::Function::Error;
        }

        types::Double* pDblOffset = in[1]->getAs<types::Double>();

        if (pDblOffset->isScalar() == false || pDblOffset->isComplex())
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : A real scalar expected.\n"), "triu", 2);
            return types::Function::Error;
        }

        iOffset = (int)pDblOffset->get(0);
    }

    /***** get data *****/
    if (in[0]->isDouble() || in[0]->isInt()) // double
    {
        types::InternalType* pOut = NULL;
        switch (in[0]->getType())
        {
            case types::InternalType::ScilabDouble:
                pOut = triu_const(in[0]->getAs<types::Double>(), iOffset);
                break;
            case types::InternalType::ScilabInt8:
                pOut = triu_const(in[0]->getAs<types::Int8>(), iOffset);
                break;
            case types::InternalType::ScilabInt16:
                pOut = triu_const(in[0]->getAs<types::Int16>(), iOffset);
                break;
            case types::InternalType::ScilabInt32:
                pOut = triu_const(in[0]->getAs<types::Int32>(), iOffset);
                break;
            case types::InternalType::ScilabInt64:
                pOut = triu_const(in[0]->getAs<types::Int64>(), iOffset);
                break;
            case types::InternalType::ScilabUInt8:
                pOut = triu_const(in[0]->getAs<types::UInt8>(), iOffset);
                break;
            case types::InternalType::ScilabUInt16:
                pOut = triu_const(in[0]->getAs<types::UInt16>(), iOffset);
                break;
            case types::InternalType::ScilabUInt32:
                pOut = triu_const(in[0]->getAs<types::UInt32>(), iOffset);
                break;
            case types::InternalType::ScilabUInt64:
                pOut = triu_const(in[0]->getAs<types::UInt64>(), iOffset);
                break;
            default:
            {} // never occurred
        }
        out.push_back(pOut);
    }
    else if (in[0]->isPoly()) // polynom
    {
        types::Polynom* pPolyIn = in[0]->getAs<types::Polynom>();
        int iRows = pPolyIn->getRows();
        int iCols = pPolyIn->getCols();
        int* piRanks = new int[iRows * iCols];
        memset(piRanks, 0x00, iRows * iCols * sizeof(int));
        types::Polynom* pPolyOut = new types::Polynom(pPolyIn->getVariableName(), iRows, iCols, piRanks);
        delete[] piRanks;
        pPolyOut->setZeros();

        for (int i = 0 ; i < iCols ; i++)
        {
            int iSize = std::min(std::max(i + 1 - iOffset, 0), iRows);
            for (int j = 0; j < iSize; j++)
            {
                int iPos = i * iRows + j;
                pPolyOut->set(iPos, pPolyIn->get(iPos));
            }
        }

        out.push_back(pPolyOut);
    }
    else
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_triu";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    return types::Function::OK;
}
Example #3
0
types::Function::ReturnValue sci_det(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::Double* pDbl             = NULL;
    types::Double* pDblMantissa     = NULL;
    types::Double* pDblExponent     = NULL;
    double* pData                   = NULL;

    if (in.size() != 1)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "det", 1);
        return types::Function::Error;
    }

    if (_iRetCount > 2)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), "det", 1, 2);
        return types::Function::Error;
    }

    if ((in[0]->isDouble() == false))
    {
        ast::ExecVisitor exec;
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_det";
        return Overload::call(wstFuncName, in, _iRetCount, out, &exec);
    }

    pDbl = in[0]->getAs<types::Double>()->clone()->getAs<types::Double>();

    if (pDbl->isComplex())
    {
        pData = (double *)oGetDoubleComplexFromPointer(pDbl->getReal(), pDbl->getImg(), pDbl->getSize());
        if (!pData)
        {
            Scierror(999, _("%s: Cannot allocate more memory.\n"), "det");
            return types::Function::Error;
        }
    }
    else
    {
        pData = pDbl->getReal();
    }

    if (pDbl->getRows() != pDbl->getCols())
    {
        Scierror(20, _("%s: Wrong type for input argument #%d: Square matrix expected.\n"), "det", 1);
        return types::Function::Error;
    }

    if ((pDbl->getRows() == -1)) // manage eye case
    {
        Scierror(271, _("%s: Size varying argument a*eye(), (arg %d) not allowed here.\n"), "det", 1);
        return types::Function::Error;
    }

    pDblMantissa = new types::Double(1, 1, pDbl->isComplex());

    if (_iRetCount == 2)
    {
        pDblExponent = new types::Double(1, 1);
    }

    int iExponent = 0;
    int iRet = iDetM(pData, pDbl->getCols(), pDblMantissa->getReal(), pDbl->isComplex() ? pDblMantissa->getImg() : NULL, pDblExponent ? &iExponent : NULL);
    if (iRet != 0)
    {
        Scierror(999, _("%s: LAPACK error n°%d.\n"), "det", iRet);
        return types::Function::Error;
    }

    if (pDblExponent)
    {
        pDblExponent->set(0, iExponent);
    }

    if (pDbl->isComplex())
    {
        vFreeDoubleComplexFromPointer((doublecomplex*)pData);
    }

    if (_iRetCount == 2)
    {
        out.push_back(pDblExponent);
    }

    delete pDbl;
    out.push_back(pDblMantissa);

    return types::Function::OK;
}
Example #4
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_mgetl(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    int iFileID                 = 0;
    int iErr                    = 0;
    bool bCloseFile             = false;
    int iLinesExcepted          = -1;
    int iLinesRead              = -1;
    wchar_t** wcReadedStrings   = NULL;

    if (in.size() < 1 || in.size() > 2)
    {
        Scierror(77, _("%s: Wrong number of input arguments: %d to %d expected.\n"), "mgetl" , 1, 2);
        return types::Function::OK;
    }

    if (in.size() == 2)
    {
        //number of lines
        if (in[1]->isDouble() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: An integer value expected.\n"), "mgetl", 2);
            return types::Function::Error;
        }

        if (in[1]->getAs<types::Double>()->isScalar() == false)
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: An integer value expected.\n"), "mgetl", 2);
            return types::Function::Error;
        }

        if (in[1]->getAs<types::Double>()->get(0) != (int)in[1]->getAs<types::Double>()->get(0))
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: An integer value expected.\n"), "mgetl", 2);
            return types::Function::Error;
        }

        iLinesExcepted = static_cast<int>(in[1]->getAs<types::Double>()->get(0));
    }

    if (in[0]->isDouble() && in[0]->getAs<types::Double>()->getSize() == 1)
    {
        iFileID = static_cast<int>(in[0]->getAs<types::Double>()->get(0));
    }
    else if (in[0]->isString() && in[0]->getAs<types::String>()->getSize() == 1)
    {
        wchar_t *expandedFileName = expandPathVariableW(in[0]->getAs<types::String>()->get(0));

        iErr = mopen(expandedFileName, L"rt", 0, &iFileID);

        if (iErr)
        {
            char* pst = wide_string_to_UTF8(expandedFileName);
            switch (iErr)
            {
                case MOPEN_NO_MORE_LOGICAL_UNIT:
                    Scierror(66, _("%s: Too many files opened!\n"), "mgetl");
                    break;
                case MOPEN_CAN_NOT_OPEN_FILE:
                    Scierror(999, _("%s: Cannot open file %s.\n"), "mgetl", pst);
                    break;
                case MOPEN_NO_MORE_MEMORY:
                    Scierror(999, _("%s: No more memory.\n"), "mgetl");
                    break;
                case MOPEN_INVALID_FILENAME:
                    Scierror(999, _("%s: invalid filename %s.\n"), "mgetl", pst);
                    break;
                default: //MOPEN_INVALID_STATUS
                    Scierror(999, _("%s: invalid status.\n"), "mgetl");
                    break;
            }

            FREE(pst);
            FREE(expandedFileName);
            return types::Function::Error;
        }
        FREE(expandedFileName);
        bCloseFile = true;
    }
    else
    {
        //Error
        Scierror(999, _("%s: Wrong type for input argument #%d: a String or Integer expected.\n"), "mgetl", 1);
        return types::Function::Error;
    }

    switch (iFileID)
    {
        case 0: // stderr
        case 6: // stdout
            Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mgetl", iFileID);
            return types::Function::Error;
        default :
        {
            types::File* pFile = FileManager::getFile(iFileID);
            // file opened with fortran open function
            if (pFile == NULL || pFile->getFileType() == 1)
            {
                Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mgetl", iFileID);
                return types::Function::Error;
            }

            wcReadedStrings = mgetl(iFileID, iLinesExcepted, &iLinesRead, &iErr);

            switch (iErr)
            {
                case MGETL_MEMORY_ALLOCATION_ERROR :
                    break;

            }
        }
    }

    if (wcReadedStrings && iLinesRead > 0)
    {
        types::String *pS = new types::String(iLinesRead, 1);
        pS->set(wcReadedStrings);
        out.push_back(pS);
        freeArrayOfWideString(wcReadedStrings, iLinesRead);
    }
    else
    {
        out.push_back(types::Double::Empty());
        if (wcReadedStrings)
        {
            FREE(wcReadedStrings);
        }
    }

    if (bCloseFile)
    {
        mclose(iFileID);
    }

    return types::Function::OK;
}
Example #5
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_atan(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::Double* pDblX   = NULL;
    types::Double* pDblY   = NULL;
    types::Double* pDblOut = NULL;

    if (in.size() < 1 || in.size() > 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "atan", 1, 2);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "atan", 1);
        return types::Function::Error;
    }

    if (in[0]->isDouble() == false)
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_atan";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    pDblX = in[0]->getAs<types::Double>();

    if (in.size() == 1)
    {
        if (pDblX->isComplex())
        {
            pDblOut = new types::Double(pDblX->getDims(), pDblX->getDimsArray(), true);
            double* pXR = pDblX->get();
            double* pXI = pDblX->getImg();
            double* pOR = pDblOut->get();
            double* pOI = pDblOut->getImg();

            int size = pDblX->getSize();
            bool msg = true;
            for (int i = 0; i < size; i++)
            {
                if (msg && pXR[i] == 0 && std::abs(pXI[i]) == 1)
                {
                    if (ConfigVariable::getIeee() == 0)
                    {
                        Scierror(999, _("%s: Wrong value for input argument #%d : Singularity of the function.\n"), "atan", 1);
                        return types::Function::Error;
                    }
                    else if (msg && ConfigVariable::getIeee() == 1)
                    {
                        if (ConfigVariable::getWarningMode())
                        {
                            sciprint(_("%s: Warning: Wrong value for input argument #%d : Singularity of the function.\n"), "atan", 1);
                        }

                        msg = false;
                    }
                }

                C2F(watan)(pXR + i, pXI + i, pOR + i, pOI + i);
            }
        }
        else
        {
            pDblOut = new types::Double(pDblX->getDims(), pDblX->getDimsArray(), false);
            double* pXR = pDblX->get();
            double* pOR = pDblOut->get();
            int size = pDblX->getSize();

            for (int i = 0; i < size; i++)
            {
                pOR[i] = std::atan(pXR[i]);
            }
        }
    }
    else // in.size() == 2
    {
        pDblY = in[1]->getAs<types::Double>();

        if (pDblX->isComplex() || pDblY->isComplex())
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : A real matrix expected.\n"), "atan", 2);
            return types::Function::Error;
        }

        if (pDblX->getSize() != pDblY->getSize())
        {
            Scierror(999, _("%s: Wrong size for input argument #%d and #%d: Same size expected.\n"), "atan", 1, 2);
            return types::Function::Error;
        }

        pDblOut = new types::Double(pDblX->getDims(), pDblX->getDimsArray(), false);
        double* pXR = pDblX->get();
        double* pYR = pDblY->get();
        double* pOR = pDblOut->get();
        int size = pDblX->getSize();

        for (int i = 0; i < size; i++)
        {
            pOR[i] =  std::atan2(pXR[i], pYR[i]);
        }
    }

    out.push_back(pDblOut);
    return types::Function::OK;
}
types::Function::ReturnValue sci_fieldnames(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    if (in.size() != 1)
    {
        Scierror(999, _("%s: Wrong number of input argument(s): %d expected.\n"), "fieldnames", 1);
        return types::Function::Error;
    }


    // FIXME : iso-functionnal to Scilab < 6
    // Works on other types except userType, {m,t}list and struct
    if (in[0]->isStruct() == false && in[0]->isMList() == false && in[0]->isTList() == false && in[0]->isUserType() == false)
    {
        out.push_back(types::Double::Empty());
        return types::Function::OK;
    }

    // STRUCT
    if (in[0]->isStruct() == true)
    {
        types::String* pFields = in[0]->getAs<types::Struct>()->getFieldNames();
        if (pFields)
        {
            out.push_back(pFields);
            //delete pFields;
        }
        else
        {
            out.push_back(types::Double::Empty());
        }
        return types::Function::OK;
    }

    types::InternalType* pIT;

    // TLIST or MLIST
    if (in[0]->isList() == true)
    {
        // We only need list capabilities to retrieve first argument as List.
        types::List *pInList = in[0]->getAs<types::List>();
        pIT = pInList->get(0);

        if (pIT == nullptr || pIT->isString() == false)
        {
            // FIXME : iso-functionnal to Scilab < 6
            // Works on other types except userType, {m,t}list and struct
            out.push_back(types::Double::Empty());
            return types::Function::OK;
        }
    }

    // USER-TYPE (typically an Xcos object)
    if (in[0]->isUserType() == true)
    {
        // We only need userType capabilities to retrieve first argument as UserType.
        types::UserType *pInUser = in[0]->getAs<types::UserType>();

        // Extract the sub-type
        std::wstring subType (pInUser->getShortTypeStr());

        // Extract the properties
        types::typed_list one(1, new types::Double(1));
        types::InternalType* pProperties = pInUser->extract(&one);
        if (pProperties == nullptr || pProperties->isString() == false)
        {
            // FIXME : iso-functionnal to Scilab < 6
            // Works on other types except userType, {m,t}list and struct
            out.push_back(types::Double::Empty());
            return types::Function::OK;
        }
        int nProp = ((types::String*) pProperties)->getSize();

        pIT = new types::String(nProp + 1, 1);
        ((types::String*) pIT)->set(0, subType.data());
        for (int i = 0; i < nProp; ++i)
        {
            ((types::String*) pIT)->set(i + 1, ((types::String*)pProperties)->get(i));
        }
    }

    types::String *pAllFields;
    if (pIT)
    {
        pAllFields = pIT->getAs<types::String>();
    }
    else
    {
        Scierror(999, _("Could not retrieve sub-type.\n"));
        return types::Function::Error;
    }
    wchar_t **pwcsAllStrings =  pAllFields->get();
    // shift to forget first value corresponding to type.
    //    ++pwcsAllStrings;


    types::String *pNewString = new types::String(pAllFields->getSize() - 1, 1, pwcsAllStrings + 1);

    out.push_back(pNewString);

    return types::Function::OK;
}
Example #7
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_hdf5_load_v3(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    std::string filename;
    int rhs = static_cast<int>(in.size());

    if (rhs < 1)
    {
        Scierror(999, _("%s: Wrong number of input argument(s): at least %d expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    if (in[0]->getId() != types::InternalType::IdScalarString)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    wchar_t* wfilename = expandPathVariableW(in[0]->getAs<types::String>()->get()[0]);
    char* cfilename = wide_string_to_UTF8(wfilename);
    filename = cfilename;
    FREE(wfilename);
    FREE(cfilename);

    int iFile = openHDF5File(filename.data(), 0);
    if (iFile < 0)
    {
        Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), filename.data());
        return types::Function::Error;
    }

    //manage version information
    int iVersion = getSODFormatAttribute(iFile);
    if (iVersion != SOD_FILE_VERSION)
    {
        //can't read file with version newer that me !
        Scierror(999, _("%s: Wrong SOD file format version. Expected: %d Found: %d\n"), fname.data(), SOD_FILE_VERSION, iVersion);
        return types::Function::Error;
    }

    if (rhs > 1)
    {
        for (int i = 1; i < rhs; ++i)
        {
            std::string var;
            char* cvar = wide_string_to_UTF8(in[i]->getAs<types::String>()->get()[0]);
            var = cvar;
            FREE(cvar);

            if (import_variable(iFile, var) == false)
            {
                Scierror(999, _("%s: Unable to load \'%s\'.\n"), fname.data(), var.data());
                return types::Function::Error;
            }
        }
    }
    else
    {
        //restore all variables
        int iNbItem = 0;
        iNbItem = getVariableNames6(iFile, NULL);
        if (iNbItem != 0)
        {
            std::vector<char*> vars(iNbItem);
            iNbItem = getVariableNames6(iFile, vars.data());
            for (auto & var : vars)
            {
                std::string s(var);
                FREE(var);
                if (import_variable(iFile, s) == false)
                {
                    Scierror(999, _("%s: Unable to load \'%s\'.\n"), fname.data(), s.data());
                    return types::Function::Error;
                }
            }
        }
    }

    //close the file
    closeHDF5File(iFile);

    out.push_back(new types::Bool(1));
    return types::Function::OK;
}
Example #8
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_roots(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    std::wstring wstrAlgo   = L"e"; // e = eigen (default), f = fast
    types::Double* pDblIn   = NULL;
    types::Double* pDblOut  = NULL;
    types::Polynom* pPolyIn = NULL;

    double* pdblInReal   = NULL;
    double* pdblInImg    = NULL;
    double* pdblTempReal = NULL;
    double* pdblTempImg  = NULL;

    int iOne = 1;
    int imOne = -1;
    int iSize = 0;
    bool bComplex = false;
    types::Function::ReturnValue ret = types::Function::OK;

    if (in.size() < 1 || in.size() > 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "roots", 1, 2);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "roots", 1);
        return types::Function::Error;
    }

    // get algo type
    if (in.size() == 2)
    {
        if (in[1]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : string expected.\n"), "roots", 2);
            return types::Function::Error;
        }

        types::String* pStrAlgo = in[1]->getAs<types::String>();
        if (pStrAlgo->isScalar() == false)
        {
            Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), "roots", 2);
            return types::Function::Error;
        }

        wstrAlgo = pStrAlgo->get(0);
        if (wstrAlgo != L"e" && wstrAlgo != L"f")
        {
            Scierror(999, _("%s: Wrong value for input argument #%d : ""%s"" or ""%s"" expected.\n"), "roots", 2, "e", "f");
            return types::Function::Error;
        }
    }

    if (in[0]->isDouble())
    {
        // for Matlab compatibility root of the vector of coefficients
        pDblIn = in[0]->getAs<types::Double>();
        if (pDblIn->isEmpty())
        {
            out.push_back(types::Double::Empty());
            return types::Function::OK;
        }

        iSize = pDblIn->getSize();

        // old fortran function dtild
        // switch elements of a vector. [1 2 3] => [3 2 1]
        pdblInReal = new double[iSize];
        C2F(dcopy)(&iSize, pDblIn->get(), &iOne, pdblInReal, &imOne);
        if (pDblIn->isComplex())
        {
            bComplex = true;
            pdblInImg = new double[iSize];
            C2F(dcopy)(&iSize, pDblIn->getImg(), &iOne, pdblInImg, &imOne);
        }
    }
    else if (in[0]->isPoly())
    {
        pPolyIn = in[0]->getAs<types::Polynom>();

        if (pPolyIn->isScalar() == false)
        {
            Scierror(999, _("%s: Wrong size for input argument #%d : A scalar expected.\n"), "roots", 1);
            return types::Function::Error;
        }

        iSize      = pPolyIn->getMaxRank() + 1;
        pdblInReal = pPolyIn->get(0)->get();

        if (pPolyIn->isComplex())
        {
            bComplex = true;
            pdblInImg = pPolyIn->get(0)->getImg();
        }
    }
    else
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_roots";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    // If "fast" algo was chosen and polynomial is complex,
    // then produce an error.
    if (wstrAlgo == L"f" && bComplex)
    {
        Scierror(999, _("%s: Wrong value for input argument #%d : If algo is ""%s"", a real is expected.\n"), "roots", 2, "f");
        return types::Function::Error;
    }

    double t = 0;
    while (t == 0)
    {
        iSize--;
        if (iSize < 0)
        {
            out.push_back(types::Double::Empty());
            return types::Function::OK;
        }

        t = std::fabs(pdblInReal[iSize]);
        if (bComplex)
        {
            t += std::fabs(pdblInImg[iSize]);
        }
    }

    // If "fast" algo was chosen and polynomial has degree greater than 100,
    // then produce an error.
    if (wstrAlgo == L"f" && iSize > 100)
    {
        Scierror(999, _("%s: Wrong value for input argument #%d : If algo is ""%s"", a degree less than %d expected.\n"), "roots", 2, "f", 100);
        return types::Function::Error;
    }

    if (wstrAlgo == L"f")
    {
        // real polynomial: rpoly algorithm
        // this alg is much more speedy, but it may happens that it gives
        // erroneous results without messages : example
        // roots(%s^31-8*%s^30+9*%s^29+0.995) should have two real roots near
        // 1.355 and 6.65 and the other ones inside a circle centered in 0
        // with radius 1

        pDblOut = new types::Double(iSize, 1, true);
        int iFail = 0;
        int iSizeP1 = iSize + 1;

        double* pdblTempReal = new double[iSize + 1];
        C2F(dcopy)(&iSizeP1, pdblInReal, &iOne, pdblTempReal, &imOne);
        C2F(rpoly)(pdblTempReal, &iSize, pDblOut->get(), pDblOut->getImg(), &iFail);
        delete pdblTempReal;

        if (iFail)
        {
            if (iFail == 1)
            {
                Scierror(999, _("%s: Convergence problem...\n"), "roots");
            }
            else if (iFail == 2)
            {
                Scierror(999, _("%s: Leading coefficient is zero.\n"), "roots");
            }
            else if (iFail == 3)
            {
                Scierror(999, _("%s: Too high degree (max 100).\n"), "roots");
            }

            return types::Function::Error;
        }

        out.push_back(pDblOut);
    }
    else // wstrAlgo == L"e"
    {
        // Companion matrix method
        int iSizeM1 = iSize - 1;
        int iSizeP1 = iSize + 1;
        double dblOne = 1;
        double* pdblTempReal = new double[iSize];
        double* pdblTempImg = NULL;
        double sr = pdblInReal[iSize];

        C2F(dcopy)(&iSize, pdblInReal, &iOne, pdblTempReal, &imOne);

        if (bComplex)
        {
            pdblTempImg = new double[iSize];
            C2F(dcopy)(&iSize, pdblInImg, &iOne, pdblTempImg, &imOne);

            double si = pdblInImg[iSize];
            double t = sr * sr + si * si;
            sr = -sr / t;
            si = si / t;

            C2F(wscal)(&iSize, &sr, &si, pdblTempReal, pdblTempImg, &iOne);
        }
        else
        {
            double dbl = -1 / sr;
            C2F(dscal)(&iSize, &dbl, pdblTempReal, &iOne);
        }

        pDblOut = new types::Double(iSize, iSize, bComplex);
        double* pdblOutReal = pDblOut->get();
        double* pdblOutImg = NULL;

        memset(pdblOutReal, 0x00, pDblOut->getSize() * sizeof(double));
        C2F(dset)(&iSizeM1, &dblOne, &pdblOutReal[iSize], &iSizeP1);
        C2F(dcopy)(&iSize, pdblTempReal, &iOne, pdblOutReal, &iOne);
        delete[] pdblTempReal;

        if (bComplex)
        {
            pdblOutImg = pDblOut->getImg();
            memset(pdblOutImg, 0x00, pDblOut->getSize() * sizeof(double));
            C2F(dcopy)(&iSize, pdblTempImg, &iOne, pdblOutImg, &iOne);
            delete[] pdblTempImg;
        }

        //call spec
        types::InternalType* pSpec = symbol::Context::getInstance()->get(symbol::Symbol(L"spec"));
        if (pSpec && pSpec->isFunction())
        {
            types::Function *funcSpec = pSpec->getAs<types::Function>();
            types::typed_list tlInput;
            types::optional_list tlOpt;
            tlInput.push_back(pDblOut);

            ret = funcSpec->call(tlInput, tlOpt, 1, out);
            pDblOut->killMe();
        }
        else
        {
            Scierror(999, _("%s: unable to find spec function\n"), "roots");
            return types::Function::Error;
        }
    }

    if (pDblIn)
    {
        delete pdblInReal;
        if (bComplex)
        {
            delete pdblInImg;
        }
    }

    return ret;
}
Example #9
0
types::Function::ReturnValue sci_string(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    if (in.size() != 1)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "string", 1);
        return types::Function::Error;
    }

    switch (in[0]->getType())
    {
        case types::GenericType::ScilabSparse:
        {
            //C=sparse([0 0 4 0 9;0 0 5 0 0;1 3 0 7 0;0 0 6 0 10;2 0 0 8 0]);string(C)
            types::Sparse* pS = in[0]->getAs<types::Sparse>();
            int iRows = pS->getRows();
            int iCols = pS->getCols();
            bool isComplex = pS->isComplex();
            std::wostringstream ostr;
            std::vector<std::wstring> vect;


            ostr << "(" << iRows << "," << iCols << ") sparse matrix";

            vect.push_back(ostr.str());
            ostr.str(L"");
            ostr.clear();

            for (int i = 0 ; i < iRows ; i++)
            {
                for (int j = 0 ; j < iCols ; j++)
                {
                    std::wostringstream temp;
                    double real = pS->getReal(i, j);
                    double cplx = 0;
                    if (isComplex)
                    {
                        cplx = pS->getImg(i, j).imag();
                    }

                    if (real || cplx )
                    {
                        temp << L"(" << i + 1 << L"," << j + 1 << L")    ";

                        if (real)
                        {
                            temp << pS->getReal(i, j);
                        }

                        if (cplx)
                        {
                            if (real && cplx > 0)
                            {
                                temp << L"+";
                            }
                            else if (cplx < 0)
                            {
                                temp << L"-";
                            }

                            temp << L"%i*" << std::abs(cplx);
                        }

                        ostr << temp.str();
                        vect.push_back(ostr.str());
                        ostr.str(L"");
                        ostr.clear();
                    }
                }
            }

            types::String* pSt = new types::String((int)vect.size(), 1);
            for (int i = 0 ; i < vect.size(); i++)
            {
                pSt->set(i, vect[i].c_str());
            }

            out.push_back(pSt);
            break;
        }

        case types::InternalType::ScilabInt8 :
        {
            return intString(in[0]->getAs<types::Int8>(), out);
        }
        case types::InternalType::ScilabUInt8 :
        {
            return intString(in[0]->getAs<types::UInt8>(), out);
        }
        case types::InternalType::ScilabInt16 :
        {
            return intString(in[0]->getAs<types::Int16>(), out);
        }
        case types::InternalType::ScilabUInt16 :
        {
            return intString(in[0]->getAs<types::UInt16>(), out);
        }
        case types::InternalType::ScilabInt32 :
        {
            return intString(in[0]->getAs<types::Int32>(), out);
        }
        case types::InternalType::ScilabUInt32 :
        {
            return intString(in[0]->getAs<types::UInt32>(), out);
        }
        case types::InternalType::ScilabInt64 :
        {
            return intString(in[0]->getAs<types::Int64>(), out);
        }
        case types::InternalType::ScilabUInt64 :
        {
            return intString(in[0]->getAs<types::UInt64>(), out);
        }
        case types::InternalType::ScilabDouble :
        {
            return doubleString(in[0]->getAs<types::Double>(), out);
        }
        case types::InternalType::ScilabString :
        {
            out.push_back(in[0]);
            break;
        }
        case types::InternalType::ScilabFunction:
        {
            Scierror(999, _("%s: Wrong type for input argument #%d.\n"), "string", 1);
            return types::Function::Error;
        }
        case types::InternalType::ScilabMacroFile :
        {
            if (_iRetCount != 3)
            {
                Scierror(77, _("%s: Wrong number of output argument(s): %d expected.\n"), "string", 3);
                return types::Function::Error;
            }

            types::MacroFile* pMF = in[0]->getAs<types::MacroFile>();
            types::InternalType* pOut = NULL;
            types::InternalType* pIn = NULL;
            types::InternalType* pBody = NULL;

            getMacroString(pMF->getMacro(), &pOut, &pIn, &pBody);

            out.push_back(pOut);
            out.push_back(pIn);
            out.push_back(pBody);
            break;
        }
        case types::InternalType::ScilabMacro :
        {
            if (_iRetCount != 3)
            {
                Scierror(77, _("%s: Wrong number of output argument(s): %d expected.\n"), "string", 3);
                return types::Function::Error;
            }

            types::Macro* pM = in[0]->getAs<types::Macro>();
            types::InternalType* pOut = NULL;
            types::InternalType* pIn = NULL;
            types::InternalType* pBody = NULL;

            getMacroString(pM, &pOut, &pIn, &pBody);

            out.push_back(pOut);
            out.push_back(pIn);
            out.push_back(pBody);
            break;
        }
        case types::InternalType::ScilabTList :
        case types::InternalType::ScilabMList :
        case types::InternalType::ScilabPolynom :
        {
            std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_string";
            return Overload::call(wstFuncName, in, _iRetCount, out);
        }
        case types::InternalType::ScilabBool:
        {
            return booleanString(in[0]->getAs<types::Bool>(), out);
        }
        case types::InternalType::ScilabLibrary:
        {
            types::Library* pL = in[0]->getAs<types::Library>();
            std::wstring path = pL->getPath();
            std::list<std::wstring> macros;
            int size = pL->getMacrosName(macros);
            types::String* pS = new types::String(size + 1, 1);
            pS->set(0, path.c_str());
            int i = 1;
            for (auto it : macros)
            {
                pS->set(i++, it.c_str());
            }

            out.push_back(pS);
            break;
        }
        case types::InternalType::ScilabImplicitList:
        {
            return implicitListString(in[0]->getAs<types::ImplicitList>(), out);
        }
        case types::InternalType::ScilabColon:
        {
            out.push_back(new types::String(L""));
            break;
        }
        default:
        {
            std::wostringstream ostr;
            in[0]->toString(ostr);
            out.push_back(new types::String(ostr.str().c_str()));
            break;
        }
    }
    return types::Function::OK;
}
Example #10
0
static types::Function::ReturnValue isdef(types::typed_list& in, int _iRetCount, types::typed_list& out, const char* fname)
{
    types::String* pStrIn = NULL;

    if (in.size() != 1 && in.size() != 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected."), fname, 1, 2);
        return types::Function::Error;
    }

    if (!in[0]->isString())
    {
        Scierror(999, _("%s: Wrong type for argument #%d: Matrix of strings expected.\n"), fname, 1);
        return types::Function::Error;
    }

    if (in.size() == 2 && (!in[1]->isString() || in[1]->getAs<types::String>()->getSize() != 1))
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A single string expected.\n"), fname, 2);
        return types::Function::Error;
    }

    const wchar_t *psScope = L"all"; // Default option is "all"
    if (in.size() == 2)
    {
        psScope = in[1]->getAs<types::String>()->get(0);
    }

    pStrIn = in[0]->getAs<types::String>();

    types::InternalType *pIT;
    types::Bool* pBOut = new types::Bool(pStrIn->getDims(), pStrIn->getDimsArray());

    switch (getScopeFromOption(psScope))
    {
        case All:
            for (int i = 0; i < pStrIn->getSize(); i++)
            {
                pIT = symbol::Context::getInstance()->get(symbol::Symbol(pStrIn->get(i)));
                pBOut->set(i, pIT != NULL && pIT->getType() != types::InternalType::ScilabVoid);
            }
            break;
        case Local:
            for (int i = 0; i < pStrIn->getSize(); i++)
            {
                pIT = symbol::Context::getInstance()->getCurrentLevel(symbol::Symbol(pStrIn->get(i)));
                pBOut->set(i, pIT != NULL && pIT->getType() != types::InternalType::ScilabVoid);
            }
            break;
        case NoLocal:
            for (int i = 0; i < pStrIn->getSize(); i++)
            {
                pIT = symbol::Context::getInstance()->getAllButCurrentLevel(symbol::Symbol(pStrIn->get(i)));
                pBOut->set(i, pIT != NULL && pIT->getType() != types::InternalType::ScilabVoid);
            }
            break;
        default:
            Scierror(36, _("%s: Wrong input argument %d.\n"), fname, 2);
            return types::Function::Error;
    }

    out.push_back(pBOut);

    return types::Function::OK;
}
Example #11
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_xls_read(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    // input
    types::Double* pDblId       = NULL;
    types::Double* pDblSheetpos = NULL;

    int iId         = 0;
    int iSheetpos   = 0;
    int rows        = 0;
    int cols        = 0;
    int iErr        = 0;

    int* ind     = NULL;
    double* data = NULL;

    // *** check the minimal number of input args. ***
    if (in.size() != 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "xls_read", 2);
        return types::Function::Error;
    }

    // *** check number of output args according the methode. ***
    if (_iRetCount != 2)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "xls_read", 2);
        return types::Function::Error;
    }

    // *** check type of input args and get it. ***
    // file id
    if (in[0]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "xls_read", 1);
        return types::Function::Error;
    }

    pDblId = in[0]->getAs<types::Double>();

    if (pDblId->isScalar() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "xls_read", 1);
        return types::Function::Error;
    }

    iId = static_cast<int>(pDblId->get(0));

    // sheetpos
    if (in[1]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "xls_read", 2);
        return types::Function::Error;
    }

    pDblSheetpos = in[1]->getAs<types::Double>();

    if (pDblSheetpos->isScalar() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A scalar expected.\n"), "xls_read", 2);
        return types::Function::Error;
    }

    iSheetpos = static_cast<int>(pDblSheetpos->get(0));

    xls_read(&iId, &iSheetpos, &data, &ind, &rows, &cols, &iErr);
    switch (iErr)
    {
        case 1 :
            Scierror(999, _("%s: No more memory.\n"), "xls_read");
            return types::Function::Error;
        case 2 :
            Scierror(999, _("%s: Failed to read expected data, may be invalid xls file.\n"), "xls_read");
            return types::Function::Error;
        case 3 :
            Scierror(999, _("%s: End of file.\n"), "xls_read");
            return types::Function::Error;
        default :
            /* no error */
            break;
    }

    if (rows * cols)
    {
        types::Double* pDblData = new types::Double(rows, cols);
        pDblData->set(data);
        types::Double* pDblInd = new types::Double(rows, cols);

        for (int i = 0; i < cols; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                pDblInd->set(j, i, static_cast<int>(ind[i * rows + j]));
            }
        }

        out.push_back(pDblData);
        out.push_back(pDblInd);

        free(data);
        free(ind);
    }
    else
    {
        out.push_back(types::Double::Empty());
        out.push_back(types::Double::Empty());
    }

    return types::Function::OK;
}
types::Function::ReturnValue sci_helpbrowser(types::typed_list &in, int _iRetCount, types::typed_list& out)
{
    int iHelpAdrSize    = 0;
    char **helpAdr      = NULL;
    char **languageAdr  = NULL;

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s:  Wrong number of output argument(s): %d to %d expected."), "helpbrowser", 0, 1);
        return types::Function::Error;
    }
    switch (in.size())
    {
        case 4:
            if (!(in[3]->isBool() == true && in[3]->getAs<types::Bool>()->isScalar() == true))
            {
                Scierror(999, _("%s:  Wrong type for input argument #%d: A boolean expected."), "helpbrowser", 4);
                return types::Function::Error;
            }
            if (!(in[2]->isString() == true && in[2]->getAs<types::String>()->isScalar() == true))
            {
                Scierror(999, _("%s:  Wrong type for input argument #%d: string expected."), "helpbrowser", 3);
                return types::Function::Error;
            }
        case 2:
            // Second argument must be String or at least [].
            if (!(in[1]->isString() == true && in[1]->getAs<types::String>()->isScalar() == true))
            {
                Scierror(999, _("%s:  Wrong type for input argument #%d: string expected."), "helpbrowser", 2);
                return types::Function::Error;
            }
            // Matrix of String or [] allowed.
            if ( !( (in[0]->isString() == true)
                || (in[0]->isDouble() == true && in[0]->getAs<types::Double>()->isEmpty() == true)))
            {
                Scierror(999, _("%s:  Wrong type for input argument #%d: string expected."), "helpbrowser", 1);
                return types::Function::Error;
            }
            break;
        default:
            Scierror(77, _("%s:  Wrong number of input argument(s): %d to %d expected."), "helpbrowser", 2, 4);
            return types::Function::Error;
    }

    /* We load SciNotes when calling javahelp because we have no way to know
     * to load it when using Javahelp because it can call SciNotes directly */
    if (!loadedDep)
    {
        loadOnUseClassPath("SciNotes");
        loadedDep = TRUE;
    }

    if (in[0]->isString() == true)
    {
        types::String *pInHelpAdr = in[0]->getAs<types::String>();
        helpAdr = new char*[pInHelpAdr->getSize()];
        iHelpAdrSize = pInHelpAdr->getSize();

        for (int i = 0 ; i < pInHelpAdr->getSize() ; ++i)
        {
            helpAdr[i] = wide_string_to_UTF8(pInHelpAdr->get(i));
        }
    }

    char* pstLang   = NULL;
    char* pstKey    = NULL;
    if (in.size() == 2)
    {
        pstLang = wide_string_to_UTF8(in[1]->getAs<types::String>()->get(0));
        launchHelpBrowser(helpAdr, iHelpAdrSize, pstLang);
    }

    if (in.size() == 4)
    {
        pstLang = wide_string_to_UTF8(in[2]->getAs<types::String>()->get(0));
        pstKey = wide_string_to_UTF8(in[1]->getAs<types::String>()->get(0));
        int iFullText = in[3]->getAs<types::Bool>()->get(0);
        searchKeyword(helpAdr, iHelpAdrSize, pstKey, pstLang, (BOOL) iFullText);
    }

    if (pstLang != NULL)
    {
        FREE(pstLang);
    }

    if (pstKey != NULL)
    {
        FREE(pstKey);
    }

    if (helpAdr != NULL) /* No toolboxes loaded */
    {
        for (int i = 0 ; i < iHelpAdrSize ; i++)
        {
            FREE(helpAdr[i]);
        }
        delete[] helpAdr;
    }

    return types::Function::OK;
}
Example #13
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_sum(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::Double* pDblIn       = NULL;
    types::Double* pDblOut      = NULL;
    types::Polynom* pPolyIn     = NULL;
    types::Polynom* pPolyOut    = NULL;

    int iOrientation    = 0;
    int iOuttype        = 1; // 1 = native | 2 = double (type of output value)

    if (in.size() < 1 || in.size() > 3)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d to %d expected.\n"), "sum", 1, 3);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "sum", 1);
        return types::Function::Error;
    }

    bool isCopy = true;
    /***** get data *****/
    switch (in[0]->getType())
    {
        case types::InternalType::ScilabDouble:
        {
            pDblIn = in[0]->getAs<types::Double>();
            isCopy = false;
            break;
        }
        case types::InternalType::ScilabBool:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::Bool>());
            iOuttype = 2;
            break;
        }
        case types::InternalType::ScilabPolynom:
        {
            pPolyIn = in[0]->getAs<types::Polynom>();
            break;
        }
        case types::InternalType::ScilabInt8:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::Int8>());
            break;
        }
        case types::InternalType::ScilabInt16:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::Int16>());
            break;
        }
        case types::InternalType::ScilabInt32:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::Int32>());
            break;
        }
        case types::InternalType::ScilabInt64:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::Int64>());
            break;
        }
        case types::InternalType::ScilabUInt8:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::UInt8>());
            break;
        }
        case types::InternalType::ScilabUInt16:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::UInt16>());
            break;
        }
        case types::InternalType::ScilabUInt32:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::UInt32>());
            break;
        }
        case types::InternalType::ScilabUInt64:
        {
            pDblIn = getAsDouble(in[0]->getAs<types::UInt64>());
            break;
        }
        default:
        {
            std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_sum";
            types::Function::ReturnValue ret = Overload::call(wstFuncName, in, _iRetCount, out);

            if (isCopy && pDblIn)
            {
                pDblIn->killMe();
            }

            return ret;
        }
    }

    if (in.size() >= 2)
    {
        if (in[1]->isDouble())
        {
            types::Double* pDbl = in[1]->getAs<types::Double>();

            if (pDbl->isScalar() == false)
            {
                if (isCopy && pDblIn)
                {
                    pDblIn->killMe();
                }

                Scierror(999, _("%s: Wrong value for input argument #%d: A positive scalar expected.\n"), "sum", 2);
                return types::Function::Error;
            }

            iOrientation = static_cast<int>(pDbl->get(0));

            if (iOrientation <= 0)
            {
                if (isCopy && pDblIn)
                {
                    pDblIn->killMe();
                }

                Scierror(999, _("%s: Wrong value for input argument #%d: A positive scalar expected.\n"), "sum", 2);
                return types::Function::Error;
            }
        }
        else if (in[1]->isString())
        {
            types::String* pStr = in[1]->getAs<types::String>();

            if (pStr->isScalar() == false)
            {
                if (isCopy && pDblIn)
                {
                    pDblIn->killMe();
                }

                Scierror(999, _("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "sum", 2);
                return types::Function::Error;
            }

            wchar_t* wcsString = pStr->get(0);

            if (wcscmp(wcsString, L"*") == 0)
            {
                iOrientation = 0;
            }
            else if (wcscmp(wcsString, L"r") == 0)
            {
                iOrientation = 1;
            }
            else if (wcscmp(wcsString, L"c") == 0)
            {
                iOrientation = 2;
            }
            else if (wcscmp(wcsString, L"m") == 0)
            {
                int iDims = 0;
                int* piDimsArray = NULL;

                if (pDblIn)
                {
                    iDims = pDblIn->getDims();
                    piDimsArray = pDblIn->getDimsArray();
                }
                else
                {
                    iDims = pPolyIn->getDims();
                    piDimsArray = pPolyIn->getDimsArray();
                }

                // old function was "mtlsel"
                for (int i = 0; i < iDims; i++)
                {
                    if (piDimsArray[i] > 1)
                    {
                        iOrientation = i + 1;
                        break;
                    }
                }
            }
            else if ((wcscmp(wcsString, L"native") == 0) && (in.size() == 2))
            {
                iOuttype = 1;
            }
            else if ((wcscmp(wcsString, L"double") == 0) && (in.size() == 2))
            {
                iOuttype = 2;
            }
            else
            {
                const char* pstrExpected = NULL;
                if (in.size() == 2)
                {
                    pstrExpected = "\"*\",\"r\",\"c\",\"m\",\"native\",\"double\"";
                }
                else
                {
                    pstrExpected = "\"*\",\"r\",\"c\",\"m\"";
                }

                if (isCopy && pDblIn)
                {
                    pDblIn->killMe();
                }

                Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "sum", 2, pstrExpected);
                return types::Function::Error;
            }
        }
        else
        {
            if (isCopy && pDblIn)
            {
                pDblIn->killMe();
            }

            Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix or a string expected.\n"), "sum", 2);
            return types::Function::Error;
        }
    }

    if (in.size() == 3)
    {
        if (in[2]->isString() == false)
        {
            if (isCopy && pDblIn)
            {
                pDblIn->killMe();
            }

            Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "sum", 3);
            return types::Function::Error;
        }

        types::String* pStr = in[2]->getAs<types::String>();

        if (pStr->isScalar() == false)
        {
            if (isCopy && pDblIn)
            {
                pDblIn->killMe();
            }

            Scierror(999, _("%s: Wrong size for input argument #%d: A scalar string expected.\n"), "sum", 3);
            return types::Function::Error;
        }

        wchar_t* wcsString = pStr->get(0);

        if (wcscmp(wcsString, L"native") == 0)
        {
            iOuttype = 1;
        }
        else if (wcscmp(wcsString, L"double") == 0)
        {
            iOuttype = 2;
        }
        else
        {
            if (isCopy && pDblIn)
            {
                pDblIn->killMe();
            }

            Scierror(999, _("%s: Wrong value for input argument #%d: %s or %s expected.\n"), "sum", 3, "\"native\"", "\"double\"");
            return types::Function::Error;
        }
    }

    /***** perform operation *****/
    if (pDblIn)
    {
        if (pDblIn->isEmpty())
        {
            if (iOrientation == 0)
            {
                out.push_back(new types::Double(0));
            }
            else
            {
                out.push_back(types::Double::Empty());
            }

            if (isCopy)
            {
                pDblIn->killMe();
            }

            return types::Function::OK;
        }
        else if (iOrientation > pDblIn->getDims())
        {
            pDblOut = pDblIn;

            if (in[0]->isBool() == false)
            {
                iOuttype = 2;
            }
        }
        else
        {
            pDblOut = sum(pDblIn, iOrientation);
            if (isCopy)
            {
                pDblIn->killMe();
            }
        }
    }
    else if (pPolyIn)
    {
        iOuttype = 1;
        if (iOrientation > pPolyIn->getDims())
        {
            pPolyOut = pPolyIn->getAs<types::Polynom>();
        }
        else
        {
            pPolyOut = sum(pPolyIn, iOrientation);
        }
    }

    /***** set result *****/
    if ((iOuttype == 1) && isCopy)
    {
        switch (in[0]->getType())
        {
            case types::InternalType::ScilabBool:
            {
                types::Bool* pB = new types::Bool(pDblOut->getDims(), pDblOut->getDimsArray());
                int* p = pB->get();
                double* pd = pDblOut->get();
                int size = pB->getSize();
                for (int i = 0; i < size; ++i)
                {
                    p[i] = pd[i] != 0 ? 1 : 0;
                }
                out.push_back(pB);
                break;
            }
            case types::InternalType::ScilabPolynom:
            {
                out.push_back(pPolyOut);
                break;
            }
            case types::InternalType::ScilabInt8:
            {
                out.push_back(toInt<types::Int8>(pDblOut));
                break;
            }
            case types::InternalType::ScilabInt16:
            {
                out.push_back(toInt<types::Int16>(pDblOut));
                break;
            }
            case types::InternalType::ScilabInt32:
            {
                out.push_back(toInt<types::Int32>(pDblOut));
                break;
            }
            case types::InternalType::ScilabInt64:
            {
                out.push_back(toInt<types::Int64>(pDblOut));
                break;
            }
            case types::InternalType::ScilabUInt8:
            {
                out.push_back(toInt<types::UInt8>(pDblOut));
                break;
            }
            case types::InternalType::ScilabUInt16:
            {
                out.push_back(toInt<types::UInt16>(pDblOut));
                break;
            }
            case types::InternalType::ScilabUInt32:
            {
                out.push_back(toInt<types::UInt32>(pDblOut));
                break;
            }
            case types::InternalType::ScilabUInt64:
            {
                out.push_back(toInt<types::UInt64>(pDblOut));
                break;
            }
            default:
                return types::Function::Error;
        }

        if (pDblOut)
        {
            pDblOut->killMe();
        }
    }
    else
    {
        out.push_back(pDblOut);
    }

    return types::Function::OK;
}
Example #14
0
types::Function::ReturnValue sci_interp(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    // input
    types::Double* pDblXp       = NULL;
    types::Double* pDblX        = NULL;
    types::Double* pDblY        = NULL;
    types::Double* pDblD        = NULL;

    // output
    types::Double* pDblYp  = NULL;
    types::Double* pDblYp1 = NULL;
    types::Double* pDblYp2 = NULL;
    types::Double* pDblYp3 = NULL;

    int iType       = 8; // default C0
    int sizeOfXp    = 0;
    int sizeOfX     = 0;

    // *** check the minimal number of input args. ***
    if (in.size() < 4 || in.size() > 5)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "interp", 4, 5);
        return types::Function::Error;
    }

    // *** check number of output args according the methode. ***
    if (_iRetCount > 4)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), "interp", 1, 4);
        return types::Function::Error;
    }

    // *** check type of input args and get it. ***
    // xp
    if (in[0]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), "interp", 1);
        return types::Function::Error;
    }

    pDblXp = in[0]->getAs<types::Double>();
    sizeOfXp = pDblXp->getSize();

    if (pDblXp->isComplex())
    {
        Scierror(999, _("%s: Wrong type for argument #%d: Real matrix expected.\n"), "interp", 1);
        return types::Function::Error;
    }

    // x
    if (in[1]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), "interp", 2);
        return types::Function::Error;
    }

    pDblX = in[1]->getAs<types::Double>();
    if ((pDblX->getCols() != 1 && pDblX->getRows() != 1) || pDblX->getSize() < 2)
    {
        Scierror(999, _("%s: Wrong size for input arguments #%d: A vector of size at least 2 expected.\n"), "interp", 2);
        return types::Function::Error;
    }
    sizeOfX = pDblX->getSize();

    if (pDblX->isComplex())
    {
        Scierror(999, _("%s: Wrong type for argument #%d: Real matrix expected.\n"), "interp", 2);
        return types::Function::Error;
    }

    // y
    if (in[2]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), "interp", 3);
        return types::Function::Error;
    }
    pDblY = in[2]->getAs<types::Double>();

    if (pDblY->isComplex())
    {
        Scierror(999, _("%s: Wrong type for argument #%d: Real matrix expected.\n"), "interp", 3);
        return types::Function::Error;
    }

    // d
    if (in[3]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d : A matrix expected.\n"), "interp", 4);
        return types::Function::Error;
    }
    pDblD = in[3]->getAs<types::Double>();

    if (pDblD->isComplex())
    {
        Scierror(999, _("%s: Wrong type for argument #%d: Real matrix expected.\n"), "interp", 4);
        return types::Function::Error;
    }

    if ( pDblX->getRows() != pDblY->getRows() ||
            pDblX->getCols() != pDblY->getCols() ||
            pDblX->getRows() != pDblD->getRows() ||
            pDblX->getCols() != pDblD->getCols())
    {
        Scierror(999, _("%s: Wrong size for input arguments #%d to #%d: Same sizes expected.\n"), "interp", 2, 4);
        return types::Function::Error;
    }

    // out mode
    if (in.size() == 5)
    {
        if (in[4]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : A string expected.\n"), "interp", 5);
            return types::Function::Error;
        }

        wchar_t* wcsType = in[4]->getAs<types::String>()->get(0);

        if (wcscmp(wcsType, L"C0") == 0)
        {
            iType = 8;
        }
        else if (wcscmp(wcsType, L"by_zero") == 0)
        {
            iType = 7;
        }
        else if (wcscmp(wcsType, L"natural") == 0)
        {
            iType = 1;
        }
        else if (wcscmp(wcsType, L"periodic") == 0)
        {
            iType = 3;
        }
        else if (wcscmp(wcsType, L"by_nan") == 0)
        {
            iType = 10;
        }
        else if (wcscmp(wcsType, L"linear") == 0)
        {
            iType = 9;
        }
        else // undefined
        {
            Scierror(999, _("%s: Wrong values for input argument #%d : '%s' is a unknow '%s' type.\n"), "interp", 5, wcsType, "outmode");
            return types::Function::Error;
        }
    }

    // *** Perform operation. ***
    pDblYp  = new types::Double(pDblXp->getRows(), pDblXp->getCols());
    pDblYp1 = new types::Double(pDblXp->getRows(), pDblXp->getCols());
    pDblYp2 = new types::Double(pDblXp->getRows(), pDblXp->getCols());
    pDblYp3 = new types::Double(pDblXp->getRows(), pDblXp->getCols());

    C2F(evalpwhermite)(pDblXp->get(), pDblYp->get(), pDblYp1->get(), pDblYp2->get(), pDblYp3->get(), &sizeOfXp, pDblX->get(), pDblY->get(), pDblD->get(), &sizeOfX, &iType);

    // *** Return result in Scilab. ***
    out.push_back(pDblYp);
    if (_iRetCount > 1)
    {
        out.push_back(pDblYp1);
    }

    if (_iRetCount > 2)
    {
        out.push_back(pDblYp2);
    }

    if (_iRetCount > 3)
    {
        out.push_back(pDblYp3);
    }

    return types::Function::OK;
}
Example #15
0
types::Function::ReturnValue sci_makecell(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    int iProd = 0;
    int iDims = 0;
    int* piDimsArray = NULL;

    if (in.size() < 2)
    {
        Scierror(999, _("%s: Wrong size for input arguments: more than %d expected.\n"), "makecell", 2);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(999, _("%s: Wrong size for output arguments: %d expected.\n"), "makecell", 1);
        return types::Function::Error;
    }

    //check data type
    switch (in[0]->getType())
    {
        case types::InternalType::ScilabDouble :
        {
            types::Double* pIn = in[0]->getAs<types::Double>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        case types::InternalType::ScilabInt8 :
        {
            types::Int8* pIn = in[0]->getAs<types::Int8>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        case types::InternalType::ScilabUInt8 :
        {
            types::UInt8* pIn = in[0]->getAs<types::UInt8>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        case types::InternalType::ScilabInt16 :
        {
            types::Int16* pIn = in[0]->getAs<types::Int16>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        case types::InternalType::ScilabUInt16 :
        {
            types::UInt16* pIn = in[0]->getAs<types::UInt16>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        case types::InternalType::ScilabInt32 :
        {
            types::Int32* pIn = in[0]->getAs<types::Int32>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        case types::InternalType::ScilabUInt32 :
        {
            types::UInt32* pIn = in[0]->getAs<types::UInt32>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        case types::InternalType::ScilabInt64 :
        {
            types::Int64* pIn = in[0]->getAs<types::Int64>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        case types::InternalType::ScilabUInt64 :
        {
            types::UInt64* pIn = in[0]->getAs<types::UInt64>();
            iDims = pIn->getSize();
            piDimsArray = new int[iDims];
            iProd = get_dimsarray(pIn->get(), iDims, piDimsArray);
            break;
        }
        default :
        {
            Scierror(999, _("%s: Wrong input arguments: Dimensions given as first argument do not match specified cell contents.\n"), "makecell");
            return types::Function::Error;
        }
    }

    //check vector format
    types::GenericType* pGT = in[0]->getAs<types::GenericType>();
    if (pGT->isScalar() || (pGT->getRows() != 1 && pGT->getCols() != 1))
    {
        delete[] piDimsArray;
        Scierror(999, _("%s: Wrong size for input argument #%d: A vector expected.\n"), "makecell", 1);
        return types::Function::Error;
    }

    //check input parameters count
    if (iProd != (in.size() - 1))
    {
        delete[] piDimsArray;
        Scierror(999, _("%s: Wrong input arguments: Dimensions given as first argument do not match specified cell contents.\n"), "makecell");
        return types::Function::Error;
    }

    types::Cell* pC = new types::Cell(iDims, piDimsArray);

    int i2dSize = piDimsArray[0] * piDimsArray[1];
    for (int i = 0; i < in.size() - 1; i++)
    {
        int idx2dStart = (i / i2dSize) * i2dSize;
        int idx2dRowMajor = (((i - idx2dStart) % piDimsArray[1]) * piDimsArray[0]) + ((i - idx2dStart) / piDimsArray[1]);
        int idx = idx2dRowMajor + idx2dStart;
        pC->set(idx , in[i + 1]);
    }

    delete[] piDimsArray;

    out.push_back(pC);
    return types::Function::OK;
}
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_findfileassociation(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::String* pS = nullptr;
    std::wstring param1;
    std::wstring param2(L"open");

    int rhs = static_cast<int>(in.size());
    if (rhs != 1 && rhs != 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), fname.data(), 1, 2);
        return types::Function::Error;
    }

    if (_iRetCount != 1)
    {
        Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    if (in[0]->isString() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    pS = in[0]->getAs<types::String>();
    if (pS->isScalar() == false)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    param1 = pS->get()[0];

    if (rhs == 2)
    {
        if (in[1]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname.data(), 2);
            return types::Function::Error;
        }

        pS = in[1]->getAs<types::String>();
        if (pS->isScalar() == false)
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), fname.data(), 2);
            return types::Function::Error;
        }

        param2 = pS->get()[0];
    }

    wchar_t* output = FindFileAssociation(param1.data(), param2.data()) ;
    if (output)
    {
        out.push_back(new types::String(output));
    }
    else
    {
        out.push_back(types::Double::Empty());
    }
    return types::Function::OK;
}
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_setdefaultlanguage(types::typed_list &in, int _piRetCount, types::typed_list &out)
{
    if (in.size() != 1)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), "setdefaultlanguage", 1);
        return types::Function::Error;
    }

    if (_piRetCount != 1)
    {
        Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), "setdefaultlanguage", 1);
        return types::Function::Error;
    }

#ifndef _MSC_VER
    /*
    ** No need to set default language except under Windows.
    ** Will return FALSE
    */
    if (getWarningMode())
    {
        sciprint(_("%ls: This feature is only supported on Windows.\n"), L"setdefaultlanguage");
    }

    types::Bool* pbOut = new types::Bool(FALSE);
    out.push_back(pbOut);
    return types::Function::OK;
#else
    if (in[0]->isString() == false || in[0]->getAs<types::String>()->getSize() != 1)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "setdefaultlanguage" , 1);
        return types::Function::Error;
    }
    wchar_t *newlang = getLanguageFromAlias(in[0]->getAs<types::String>()->get(0));

    if ( !isValidLanguage(newlang) )
    {
        if ( getWarningMode() )
        {
            sciprint(_("Unsupported language '%ls'.\n"), newlang);
        }
        out.push_back(new types::Bool(FALSE));

        return types::Function::OK;
    }
    else
    {
        wchar_t *savedLanguage = getLanguagePreferences();
        if ( wcscmp(newlang, savedLanguage) == 0 )
        {
            /* do nothing */
            out.push_back(new types::Bool(TRUE));

            return types::Function::OK;
        }
        else
        {
            // ??                if (savedLanguage) { FREE(savedLanguage); savedLanguage = NULL; }
            if ( !setlanguage(newlang) ) /* */
            {
                out.push_back(new types::Bool(FALSE));
                return types::Function::OK;
            }
            else
            {
                if ( getWarningMode() )
                {
                    sciprint("\n");
                    sciprint(_("The language for menus cannot be changed on a running console.\n"));
                    sciprint(_("Restart Scilab to apply to menus.\n"));
                }
                if ( setLanguagePreferences() )
                {
                    out.push_back(new types::Bool(TRUE));
                }
                else
                {
                    out.push_back(new types::Bool(FALSE));
                }
                return types::Function::OK;
            }
        }
    }

#endif
}
Example #18
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_readmps(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::String* pStrFileName = NULL;

    double dLowBnd  = 0;
    double dUpBnd   = 0;
    int iMaxM       = 0;
    int iMaxN       = 0;
    int iMaxNza     = 0;
    int iM          = 0;
    int iN          = 0;
    int iNza        = 0;
    int lunit       = 0; // file unit. 0 mean we open the file by this name.
    int mlunit      = 0;
    int piMode[2]   = { -1, 0};
    int ierr        = 0;
    int line        = 0;
    char typrow[2];

    wchar_t* wcsFileName = NULL;
    char* strErrorBuf = new char[bsiz];

    double big = NumericConstants::double_max;

    if (in.size() < 2 || in.size() > 3)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "readmps", 2, 3);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "readmps", 1);
        return types::Function::Error;
    }

    /*** get inputs arguments ***/
    // get file name
    if (in[0]->isString() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "readmps", 1);
        return types::Function::Error;
    }

    pStrFileName = in[0]->getAs<types::String>();

    if (pStrFileName->isScalar() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A scalar string expected.\n"), "readmps", 1);
        return types::Function::Error;
    }

    // get Bounds
    if (in[1]->isDouble() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A real vector expected.\n"), "readmps", 2);
        return types::Function::Error;
    }

    types::Double* pDblBounds = in[1]->getAs<types::Double>();

    if (pDblBounds->isComplex())
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A real vector expected.\n"), "readmps", 2);
        return types::Function::Error;
    }

    if (pDblBounds->getSize() != 2)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: A real vector of size %d expected.\n"), "readmps", 2, 2);
        return types::Function::Error;
    }

    dLowBnd = pDblBounds->get(0);
    dUpBnd  = pDblBounds->get(1);

    if (in.size() == 3)
    {
        // get Max sizes
        if (in[2]->isDouble() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A real vector expected.\n"), "readmps", 3);
            return types::Function::Error;
        }

        types::Double* pDblMaxSizes = in[2]->getAs<types::Double>();

        if (pDblMaxSizes->isComplex())
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A real vector expected.\n"), "readmps", 3);
            return types::Function::Error;
        }

        if (pDblMaxSizes->getSize() != 3)
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: A real vector of size %d expected.\n"), "readmps", 3, 3);
            return types::Function::Error;
        }

        iMaxM   = (int)pDblMaxSizes->get(0);
        iMaxN   = (int)pDblMaxSizes->get(1);
        iMaxNza = (int)pDblMaxSizes->get(2);
    }
    else
    {
        if (openMPSFile(pStrFileName, piMode, &lunit))
        {
            return types::Function::Error;
        }

        C2F(rdmpsz)(&lunit, &iMaxM, &iMaxN, &iMaxNza, &ierr, typrow, &line);
        mlunit = -lunit;
        C2F(clunit)(&mlunit, NULL, piMode, 0);

        if (ierr == 1)
        {
            Scierror(998, _("%s: Error while reading line %d.\n"), "readmps", line);
            return types::Function::Error;
        }
        else if (ierr == 2)
        {
            Scierror(999, _("%s: Unknown row type %s at line %d.\n"), "readmps", typrow, line);
            return types::Function::Error;
        }

        lunit = 0;
    }

    iM   = iMaxM;
    iN   = iMaxN;
    iNza = iMaxNza;

    /*** read MPS file ***/
    if (openMPSFile(pStrFileName, piMode, &lunit))
    {
        return types::Function::Error;
    }

    // alloc data
    char nameb[9]  = "        ";
    char namec[9]  = "        ";
    char namran[9] = "        ";
    char nambnd[9] = "        ";
    char nammps[9] = "        ";

    char* pstrRwName = new char[8 * iM + 1];
    pstrRwName[8 * iM] = '\0';
    char* pstrClName = new char[8 * iN + 1];
    pstrClName[8 * iN] = '\0';

    int irobj       = 0;
    int* piStavar   = new int[iN];
    int* piRwstat   = new int[iM];
    int* piRowcod   = new int[2 * iM];
    int* piColcod   = new int[2 * iN];
    int* piRwnmbs   = new int[iNza];
    int* piClpnts   = new int[iN + 1];
    int* piRow      = new int[iN];

    types::Double* pDblCoef     = new types::Double(iNza, 1);
    types::Double* pDblRhsb     = new types::Double(iM, 1);
    types::Double* pDblRanges   = new types::Double(iM, 1);
    types::Double* pDblBnds     = new types::Double(iN, 2);
    double* pdblRelt            = new double[iN];

    C2F(rdmps1)(&ierr, strErrorBuf, &iMaxM, &iMaxN, &iMaxNza,
                &iM, &iN, &iNza, &irobj, &big, &dLowBnd, &dUpBnd,
                namec, nameb, namran, nambnd, nammps, &lunit,
                pstrRwName, pstrClName,
                piStavar, piRwstat,
                piRowcod, piRowcod + iM,
                piColcod, piColcod + iN,
                piRwnmbs, piClpnts, piRow,
                pDblCoef->get(), pDblRhsb->get(), pDblRanges->get(),
                pDblBnds->get() + iN, pDblBnds->get(), pdblRelt,
                bsiz, 8L, 8L, 8L, 8L, 8L);

    delete piRow;
    delete pdblRelt;

    mlunit = -lunit;
    C2F(clunit)(&mlunit, NULL, piMode, 0);

    if (ierr)
    {
        int iLen = 4096;
        char* str = strErrorBuf + 4095;
        while (*str == ' ')
        {
            iLen--;
            str--;
        }

        strErrorBuf[iLen] = '\0';
        Scierror(999, "%s", strErrorBuf);
        delete[] strErrorBuf;
        return types::Function::Error;
    }

    delete[] strErrorBuf;
    /*** return output arguments ***/
    types::String* pStr = NULL;
    types::Double* pDbl = NULL;
    double* pdbl        = NULL;

    types::TList* pTL = new types::TList();
    pStr = new types::String(1, 19);
    pStr->set(0,  L"mps");
    pStr->set(1,  L"irobj");
    pStr->set(2,  L"namec");
    pStr->set(3,  L"nameb");
    pStr->set(4,  L"namran");
    pStr->set(5,  L"nambnd");
    pStr->set(6,  L"name");
    pStr->set(7,  L"rownames");
    pStr->set(8,  L"colnames");
    pStr->set(9,  L"rowstat");
    pStr->set(10, L"rowcode");
    pStr->set(11, L"colcode");
    pStr->set(12, L"rownmbs");
    pStr->set(13, L"colpnts");
    pStr->set(14, L"acoeff");
    pStr->set(15, L"rhs");
    pStr->set(16, L"ranges");
    pStr->set(17, L"bounds");
    pStr->set(18, L"stavar");
    pTL->append(pStr);

    pTL->append(new types::Double((double)irobj));
    pTL->append(new types::String(namec));
    pTL->append(new types::String(nameb));
    pTL->append(new types::String(namran));
    pTL->append(new types::String(nambnd));
    pTL->append(new types::String(nammps));

    pStr = new types::String(iM, 1);
    for (int i = 0; i < iM; i++)
    {
        char pstrTemp[9];
        memcpy(pstrTemp, pstrRwName + i * 8, 8);
        pstrTemp[8] = '\0';
        pStr->set(i, pstrTemp);
    }
    pTL->append(pStr);
    delete pstrRwName;

    pStr = new types::String(1, iN);
    for (int i = 0; i < iN; i++)
    {
        char pstrTemp[9];
        memcpy(pstrTemp, pstrClName + i * 8, 8);
        pstrTemp[8] = '\0';
        pStr->set(i, pstrTemp);
    }
    pTL->append(pStr);
    delete pstrClName;

    pDbl = new types::Double(iM, 1);
    pdbl = pDbl->get();
    for (int i = 0; i < iM; i++)
    {
        pdbl[i] = (double)piRwstat[i];
    }
    pTL->append(pDbl);
    delete piRwstat;

    pDbl = new types::Double(iM, 2);
    pdbl = pDbl->get();
    for (int i = 0; i < iM * 2; i++)
    {
        pdbl[i] = (double)piRowcod[i];
    }
    pTL->append(pDbl);
    delete piRowcod;

    pDbl = new types::Double(iN, 2);
    pdbl = pDbl->get();
    for (int i = 0; i < iN * 2; i++)
    {
        pdbl[i] = (double)piColcod[i];
    }
    pTL->append(pDbl);
    delete piColcod;

    pDbl = new types::Double(iNza, 1);
    pdbl = pDbl->get();
    for (int i = 0; i < iNza; i++)
    {
        pdbl[i] = (double)piRwnmbs[i];
    }
    pTL->append(pDbl);
    delete piRwnmbs;

    pDbl = new types::Double(1, iN + 1);
    pdbl = pDbl->get();
    for (int i = 0; i < iN + 1; i++)
    {
        pdbl[i] = (double)piClpnts[i];
    }
    pTL->append(pDbl);
    delete piClpnts;

    pTL->append(pDblCoef);
    pTL->append(pDblRhsb);
    pTL->append(pDblRanges);
    pTL->append(pDblBnds);

    pDbl = new types::Double(iN, 1);
    pdbl = pDbl->get();
    for (int i = 0; i < iN; i++)
    {
        pdbl[i] = (double)piStavar[i];
    }
    pTL->append(pDbl);
    delete piStavar;

    out.push_back(pTL);
    return types::Function::OK;
}
Example #19
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_acos(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::Double* pDblIn   = NULL;
    types::Double* pDblOut  = NULL;

    if (in.size() != 1)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "acos", 1);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "acos", 1);
        return types::Function::Error;
    }

    if (in[0]->isDouble() == false)
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_acos";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    pDblIn = in[0]->getAs<types::Double>();

    if (pDblIn->isComplex())
    {
        pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray(), true);
        int size = pDblIn->getSize();

        double* pInR = pDblIn->get();
        double* pInI = pDblIn->getImg();
        double* pOutR = pDblOut->get();
        double* pOutI = pDblOut->getImg();

        for (int i = 0 ; i < size ; i++)
        {
            C2F(wacos)(pInR + i, pInI + i, pOutR + i, pOutI + i);
        }
    }
    else
    {
        bool bOutSide = 0;
        //check if all variables are between [-1,1]
        double* pInR = pDblIn->get();
        int size = pDblIn->getSize();
        for (int i = 0; i < size; i++)
        {
            if (std::abs(pInR[i]) > 1)
            {
                bOutSide = 1;
                break;
            }
        }

        if (bOutSide) // Values outside [-1,1]
        {
            pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray(), true);
            double* pOutR = pDblOut->get();
            double* pOutI = pDblOut->getImg();
            double zero = 0;
            for (int i = 0; i < size; i++)
            {
                C2F(wacos)(pInR + i, &zero, pOutR + i, pOutI + i);
            }
        }
        else //all values are in [-1,1]
        {
            pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray(), false);
            double* pOutR = pDblOut->get();
            for (int i = 0; i < size; i++)
            {
                pOutR[i] = std::acos(pInR[i]);
            }
        }
    }

    out.push_back(pDblOut);
    return types::Function::OK;
}
Example #20
0
/*------------------------------------------------------------------------*/
types::Function::ReturnValue sci_grep(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    bool bRegularExpression = false;

    //check input paramters
    if (in.size() < 2 || in.size() > 3)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d or %d expected.\n"), "grep", 2, 3);
        return types::Function::Error;
    }

    if (in[0]->isDouble() && in[0]->getAs<types::Double>()->getSize() == 0)
    {
        types::Double *pD = types::Double::Empty();
        out.push_back(pD);
        return types::Function::OK;
    }

    if (in.size() == 3)
    {
        //"r" for regular expression
        if (in[2]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "grep", 3);
            return types::Function::Error;
        }

        types::String* pS = in[2]->getAs<types::String>();
        if (pS->getSize() != 1)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "grep", 3);
            return types::Function::Error;
        }

        if (pS->get(0)[0] == 'r')
        {
            bRegularExpression = true;
        }
    }

    if (in[0]->isString() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "grep", 1);
        return types::Function::Error;
    }

    if (in[1]->isString() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "grep", 2);
        return types::Function::Error;
    }

    types::String* pS1 = in[0]->getAs<types::String>();
    types::String* pS2 = in[1]->getAs<types::String>();


    for (int i = 0 ; i < pS2->getSize() ; i++)
    {
        if (wcslen(pS2->get(i)) == 0)
        {
            Scierror(249, _("%s: Wrong values for input argument #%d: Non-empty strings expected.\n"), "grep", 2);
            return types::Function::Error;
        }
    }

    GREPRESULTS grepresults;
    int code_error_grep = GREP_OK;

    grepresults.currentLength = 0;
    grepresults.sizeArraysMax = 0;
    grepresults.positions = NULL;
    grepresults.values = NULL;

    char** pStr1 = (char**)MALLOC(sizeof(char*) * pS1->getSize());
    for (int i = 0 ; i < pS1->getSize() ; i++)
    {
        pStr1[i] = wide_string_to_UTF8(pS1->get(i));
    }

    char** pStr2 = (char**)MALLOC(sizeof(char*) * pS2->getSize());
    for (int i = 0 ; i < pS2->getSize() ; i++)
    {
        pStr2[i] = wide_string_to_UTF8(pS2->get(i));
    }

    if (bRegularExpression)
    {
        code_error_grep = GREP_NEW(&grepresults, pStr1, pS1->getSize(), pStr2, pS2->getSize());
    }
    else
    {
        code_error_grep = GREP_OLD(&grepresults, pStr1, pS1->getSize(), pStr2, pS2->getSize());
    }

    for (int i = 0; i < pS1->getSize(); i++)
    {
        FREE(pStr1[i]);
    }
    FREE(pStr1);

    for (int i = 0; i < pS2->getSize(); i++)
    {
        FREE(pStr2[i]);
    }
    FREE(pStr2);

    switch (code_error_grep)
    {
        case GREP_OK :
        {
            types::Double* pD1 = NULL;
            if (grepresults.currentLength == 0)
            {
                pD1 = types::Double::Empty();
            }
            else
            {
                pD1 = new types::Double(1, grepresults.currentLength);
                double* pDbl1 = pD1->getReal();
                for (int i = 0 ; i < grepresults.currentLength ; i++ )
                {
                    pDbl1[i] = static_cast<double>(grepresults.values[i]);
                }
            }

            out.push_back(pD1);

            if (_iRetCount == 2)
            {
                types::Double* pD2 = NULL;
                if (grepresults.currentLength == 0)
                {
                    pD2 = types::Double::Empty();
                }
                else
                {
                    pD2 = new types::Double(1, grepresults.currentLength);
                    double* pDbl2 = pD2->getReal();
                    for (int i = 0 ; i < grepresults.currentLength ; i++ )
                    {
                        pDbl2[i] = static_cast<double>(grepresults.positions[i]);
                    }
                }

                out.push_back(pD2);
            }

            if (grepresults.values)
            {
                FREE(grepresults.values);
                grepresults.values = NULL;
            }
            if (grepresults.positions)
            {
                FREE(grepresults.positions);
                grepresults.positions = NULL;
            }
        }
        break;

        case MEMORY_ALLOC_ERROR :
            Scierror(999, _("%s: No more memory.\n"), "grep");
        //no break, to free reserved memory.
        case GREP_ERROR :
        {
            if (grepresults.values)
            {
                FREE(grepresults.values);
                grepresults.values = NULL;
            }
            if (grepresults.positions)
            {
                FREE(grepresults.positions);
                grepresults.positions = NULL;
            }
            return types::Function::Error;
        }
        break;
    }

    return types::Function::OK;
}
Example #21
0
/*--------------------------------------------------------------------------*/
Function::ReturnValue sci_load(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    if (in.size() < 1)
    {
        Scierror(999, _("%s: Wrong number of input argument(s): at least %d expected.\n"), fname.data(), 1);
        return types::Function::Error;
    }

    InternalType* pIT = in[0];
    if (pIT->getId() != InternalType::IdScalarString)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname.data(), 1);
        return Function::Error;
    }

    String *pS = pIT->getAs<types::String>();
    wchar_t* pwstPathLib = expandPathVariableW(pS->get(0));
    char* pstPath = wide_string_to_UTF8(pwstPathLib);
    if (FileExist(pstPath))
    {
        if (isHDF5File(pstPath))
        {
            FREE(pstPath);
            FREE(pwstPathLib);

            //call overload
            std::wstring wstFuncName = L"%_sodload";
            ast::ExecVisitor exec;
            Callable::ReturnValue Ret = Callable::Error;
            Ret = Overload::call(wstFuncName, in, _iRetCount, out, &exec);
            return Ret;
        }
        else
        {
            int err = 0;
            Library* lib = loadlib(pS->get(0), &err);
            FREE(pstPath);

            switch (err)
            {
                case 0:
                    //no error
                    break;
                case 1:
                {
                    char* pstPath = wide_string_to_UTF8(pS->get(0));
                    Scierror(999, _("%s: %s is not a valid module file.\n"), fname.data(), pstPath);
                    FREE(pstPath);
                    return Function::Error;
                }
                case 2:
                {
                    Scierror(999, "%s: %s", fname.data(), _("Redefining permanent variable.\n"));
                    return Function::Error;
                }
                default:
                {
                    //nothing
                }
            }

            FREE(pwstPathLib);
            lib->killMe();
        }
    }
    else
    {
        Scierror(999, _("%s: Unable to open file: \"%ls\".\n"), fname.data(), pwstPathLib);
        FREE(pstPath);
        FREE(pwstPathLib);
        return Function::Error;
    }

    return Function::OK;
}
Example #22
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_find(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    int iMax = -1;
    if (in.size() > 2)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "find", 1, 2);
        return types::Function::Error;
    }

    if (in.size() == 2)
    {
        if (in[1]->isDouble() == false || in[1]->getAs<types::Double>()->isScalar() == false || in[1]->getAs<types::Double>()->get(0) <= 0)
        {
            Scierror(999, _("%s:  Wrong type for input argument #%d: Scalar positive integer expected.\n"), "find", 2);
            return types::Function::Error;
        }

        iMax = (int)in[1]->getAs<types::Double>()->get(0);
    }

    int* piIndex = 0;
    int iValues = 0;

    if (in[0]->isGenericType() == false)
    {
        //call overload for other types
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_find";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    types::GenericType* pGT = in[0]->getAs<types::GenericType>();
    piIndex = new int[pGT->getSize()];

    if (in[0]->isBool())
    {
        types::Bool* pB = in[0]->getAs<types::Bool>();
        for (int i = 0 ; (iMax == -1 || iValues < iMax) && i < pB->getSize() ; i++)
        {
            if (pB->get(i))
            {
                piIndex[iValues] = i;
                iValues++;
            }
        }
    }
    else if (in[0]->isDouble())
    {
        types::Double* pD = in[0]->getAs<types::Double>();
        for (int i = 0 ; (iMax == -1 || iValues < iMax) && i < pD->getSize() ; i++)
        {
            if (pD->get(i))
            {
                piIndex[iValues] = i;
                iValues++;
            }
        }
    }
    else if (in[0]->isSparse())
    {
        types::Sparse* pSP = in[0]->getAs<types::Sparse>();
        int iNNZ = (int)pSP->nonZeros();
        int iRows = pSP->getRows();
        int* pRows = new int[iNNZ * 2];
        pSP->outputRowCol(pRows);
        int *pCols = pRows + iNNZ;

        for (int i = 0 ; (iMax == -1 || iValues < iMax) && i < iNNZ ; i++)
        {
            piIndex[iValues] = (pCols[i] - 1) * iRows + (pRows[i] - 1);
            iValues++;
        }

        delete[] pRows;
    }
    else if (in[0]->isSparseBool())
    {
        types::SparseBool* pSB = in[0]->getAs<types::SparseBool>();
        int iNNZ = (int)pSB->nbTrue();
        int iRows = pSB->getRows();

        int* pRows = new int[iNNZ * 2];
        pSB->outputRowCol(pRows);
        int* pCols = pRows + iNNZ;

        for (int i = 0 ; (iMax == -1 || iValues < iMax) && i < iNNZ ; i++)
        {
            piIndex[iValues] = (pCols[i] - 1) * iRows + (pRows[i] - 1);
            iValues++;
        }

        delete[] pRows;
    }
    else
    {
        delete[] piIndex;

        //call overload for other types
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_find";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    if (iValues == 0)
    {
        for (int i = 0 ; i < _iRetCount ; i++)
        {
            out.push_back(types::Double::Empty());
        }
    }
    else
    {
        int* piRefDims = pGT->getDimsArray();
        int iRefDims = pGT->getDims();

        int* piDims = new int[_iRetCount];
        int iDims = _iRetCount;

        if (iDims == iRefDims)
        {
            for (int i = 0 ; i < iRefDims ; i++)
            {
                piDims[i] = piRefDims[i];
            }
        }
        else if (iDims > iRefDims)
        {
            for (int i = 0 ; i < iRefDims ; i++)
            {
                piDims[i] = piRefDims[i];
            }

            for (int i = iRefDims ; i < iDims ; i++)
            {
                piDims[i] = 1;
            }
        }
        else //iDims < iRefDims
        {
            for (int i = 0 ; i < iDims - 1 ; i++)
            {
                piDims[i] = piRefDims[i];
            }

            piDims[iDims - 1] = 1;
            for (int i = iDims - 1 ; i < iRefDims ; i++)
            {
                piDims[iDims - 1] *= piRefDims[i];
            }
        }

        int** piCoord = new int*[iValues];
        for (int i = 0 ; i < iValues ; i++)
        {
            piCoord[i] = new int[_iRetCount];
        }

        for (int i = 0 ; i < iValues ; i++)
        {
            getCoordFromIndex(piIndex[i], piCoord[i], piDims, iDims);
        }

        for (int i = 0 ; i < _iRetCount ; i++)
        {
            types::Double* pOut = new types::Double(1, iValues);
            for (int j = 0 ; j < iValues ; j++)
            {
                pOut->set(j, piCoord[j][i] + 1);
            }
            out.push_back(pOut);
        }

        delete[] piDims;
        for (int i = 0 ; i < iValues ; i++)
        {
            delete[] piCoord[i];
        }
        delete[] piCoord;
    }

    delete[] piIndex;
    return types::Function::OK;
}
Example #23
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_floor(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    if (in.size() != 1)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "floor", 1);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "floor", 1);
        return types::Function::Error;
    }

    if (in[0]->isDouble())
    {
        types::Double* pDblIn = in[0]->getAs<types::Double>();
        types::Double* pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray(), pDblIn->isComplex());

        double* pInR = pDblIn->get();
        double* pOutR = pDblOut->get();
        int size = pDblIn->getSize();

        if (pDblIn->isComplex())
        {
            double* pInI = pDblIn->getImg();
            double* pOutI = pDblOut->getImg();
            for (int i = 0; i < size; i++)
            {
                pOutR[i] = std::floor(pInR[i]);
                pOutI[i] = std::floor(pInI[i]);
            }
        }
        else
        {
            for (int i = 0; i < size; i++)
            {
                pOutR[i] = std::floor(pInR[i]);
            }
        }

        out.push_back(pDblOut);
    }
    else if (in[0]->isSparse())
    {
        types::Sparse* pSparseIn = in[0]->getAs<types::Sparse>();
        types::Sparse* pSparseOut = new types::Sparse(pSparseIn->getRows(), pSparseIn->getCols(), pSparseIn->isComplex());

        int const nonZeros = static_cast<int>(pSparseIn->nonZeros());
        int* pRows = new int[nonZeros * 2];
        pSparseIn->outputRowCol(pRows);
        int* pCols = pRows + nonZeros;

        double* pNonZeroR = new double[nonZeros];
        double* pNonZeroI = new double[nonZeros];
        pSparseIn->outputValues(pNonZeroR, pNonZeroI);

        if (pSparseIn->isComplex())
        {
            for (int i = 0; i < nonZeros; i++)
            {
                std::complex<double> cplx(dfloors(pNonZeroR[i]), dfloors(pNonZeroI[i]));
                pSparseOut->set(pRows[i] - 1, pCols[i] - 1, cplx, false);
            }
        }
        else
        {
            for (int i = 0; i < nonZeros; i++)
            {
                pSparseOut->set(pRows[i] - 1, pCols[i] - 1, dfloors(pNonZeroR[i]), false);
            }
        }

        pSparseOut->finalize();

        delete[] pRows;
        delete[] pNonZeroR;
        delete[] pNonZeroI;

        out.push_back(pSparseOut);
    }
    else if (in[0]->isPoly())
    {
        types::Polynom* pPolyIn = in[0]->getAs<types::Polynom>();
        types::Polynom* pPolyOut = new types::Polynom(pPolyIn->getVariableName(), pPolyIn->getDims(), pPolyIn->getDimsArray());

        double* dataImg  = NULL;
        double* dataReal = NULL;

        if (pPolyIn->isComplex())
        {
            for (int i = 0; i < pPolyIn->getSize(); i++)
            {
                int rank = pPolyIn->get(i)->getRank();
                types::SinglePoly* pSP = new types::SinglePoly(&dataReal, &dataImg, rank);

                for (int j = 0; j < rank + 1; j++)
                {
                    dataReal[j] = dfloors(pPolyIn->get(i)->get()[j]);
                    dataImg[j]  = dfloors(pPolyIn->get(i)->getImg()[j]);
                }

                pPolyOut->set(i, pSP);
                delete pSP;
                pSP = NULL;
            }
        }
        else
        {
            for (int i = 0; i < pPolyIn->getSize(); i++)
            {
                int rank = pPolyIn->get(i)->getRank();
                types::SinglePoly* pSP = new types::SinglePoly(&dataReal, rank);

                for (int j = 0; j < rank + 1; j++)
                {
                    dataReal[j] = dfloors(pPolyIn->get(i)->get()[j]);
                }

                pPolyOut->set(i, pSP);
                delete pSP;
                pSP = NULL;
            }
        }

        out.push_back(pPolyOut);
    }
    else if (in[0]->isInt())
    {
        out.push_back(in[0]);
    }
    else
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_floor";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    return types::Function::OK;
}
Example #24
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_xset(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    wchar_t* pwcsWhat = NULL;
    std::list<types::Double*> lpDblInputs;
    int iSubwinUID = 0;

    if (in.size() == 0)
    {
        return Overload::call(L"%_xset", in, _iRetCount, out);
    }

    if (in.size() > 6)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "xset", 1, 6);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "xset", 1);
        return types::Function::Error;
    }

    if (in[0]->isString() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A single string expected.\n"), "xset", 1);
        return types::Function::Error;
    }

    types::String* pStr = in[0]->getAs<types::String>();

    if (pStr->isScalar() == false)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A single string expected.\n"), "xset", 1);
        return types::Function::Error;
    }

    pwcsWhat = pStr->get(0);

    if (ConfigGraphicVariable::bPropertyFound(pwcsWhat) == false)
    {
        char* pstWhat = wide_string_to_UTF8(pwcsWhat);
        Scierror(999, _("%s: Unrecognized input argument: '%s'.\n"), "xset", pstWhat);
        FREE(pstWhat);
        return types::Function::Error;
    }

    // Only in case of "fpf" and "auto clear", the second argument is a string
    // Only "default" case have one input argument
    if (ConfigGraphicVariable::getPropertyValue(pwcsWhat) != 15 && // fpf
            ConfigGraphicVariable::getPropertyValue(pwcsWhat) != 2  && // auto clear
            ConfigGraphicVariable::getPropertyValue(pwcsWhat) != 10)   // default
    {
        for (unsigned int i = 1 ; i < in.size() ; i++)
        {
            if (in[i]->isDouble() == false)
            {
                Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), "xset", i + 1);
            }
        }
    }

    switch (ConfigGraphicVariable::getPropertyValue(pwcsWhat))
    {
        case 15 : // fpf
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            if (in[1]->isString() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A single string expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            types::String* pStrValue = in[1]->getAs<types::String>();
            if (pStrValue->isScalar() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A single string expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            ConfigGraphicVariable::setFPF(pStrValue->get(0));
        }
        break;
        case 2 : // auto clear
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            if (in[1]->isString() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A single string expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            types::String* pStrValue = in[1]->getAs<types::String>();
            if (pStrValue->isScalar() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A single string expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int bAutoClear = 0;
            if (wcscmp(pStrValue->get(0), L"on") == 0)
            {
                bAutoClear = 1;
            }

            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_AUTO_CLEAR__, &bAutoClear, jni_bool, 1);
        }
        break;
        case 5 : // clipping
        {
            int clipState = 2;
            double dvalues[4];
            if (in.size() == 2)
            {
                types::Double* pDblArg = in[1]->getAs<types::Double>();
                if (pDblArg->getSize() != 4)
                {
                    Scierror(999, _("%s: Wrong size for input argument #%d: A %d-element vector expected.\n"), "xset", 2, 4);
                    return types::Function::Error;
                }

                memcpy(dvalues, pDblArg->get(), 4 * sizeof(double));
            }
            else if (in.size() != 5)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d or %d expected.\n"), "xset", 2, 5);
                return types::Function::Error;
            }
            else
            {
                for (int i = 0; i < 4 ; i++)
                {
                    dvalues[i] = in[i + 1]->getAs<types::Double>()->get(0);
                }
            }

            iSubwinUID = getOrCreateDefaultSubwin();
            setGraphicObjectProperty(iSubwinUID, __GO_CLIP_BOX__, dvalues, jni_double_vector, 4);
            setGraphicObjectProperty(iSubwinUID, __GO_CLIP_STATE__, &clipState, jni_int, 1);
        }
        break;
        case 8 : // colormap
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            types::Double* pDblArg = in[1]->getAs<types::Double>();
            getOrCreateDefaultSubwin();
            setGraphicObjectProperty(getCurrentFigure(), __GO_COLORMAP__, pDblArg->get(), jni_double_vector, pDblArg->getSize());
        }
        break;
        case 21 : // mark size
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int markSize = (int)in[1]->getAs<types::Double>()->get(0);
            int markSizeUnit = 1; /* force switch to tabulated mode : old syntax / 0 : point, 1 : tabulated */

            iSubwinUID = getOrCreateDefaultSubwin();
            setGraphicObjectProperty(iSubwinUID, __GO_MARK_SIZE_UNIT__, &markSizeUnit, jni_int, 1);
            setGraphicObjectProperty(iSubwinUID, __GO_MARK_SIZE__, &markSize, jni_int, 1);
        }
        break;
        case 20 : // mark
        {
            if (in.size() != 3)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 3);
                return types::Function::Error;
            }

            int markStyle = (int) in[1]->getAs<types::Double>()->get(0);
            int markSize = (int) in[2]->getAs<types::Double>()->get(0);
            int markSizeUnit = 1; /* force switch to tabulated mode : old syntax / 0 : point, 1 : tabulated */
            int markMode = 1;

            iSubwinUID = getOrCreateDefaultSubwin();
            setGraphicObjectProperty(iSubwinUID, __GO_MARK_MODE__, &markMode, jni_bool, 1);
            setGraphicObjectProperty(iSubwinUID, __GO_MARK_SIZE_UNIT__, &markSizeUnit, jni_int, 1); /* force switch to tabulated mode : old syntax */
            setGraphicObjectProperty(iSubwinUID, __GO_MARK_STYLE__, &markStyle, jni_int, 1);
            setGraphicObjectProperty(iSubwinUID, __GO_MARK_SIZE__, &markSize, jni_int, 1);
        }
        break;
        case 13 : // font size
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }
            double fontSize = in[1]->getAs<types::Double>()->get(0);
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_FONT_SIZE__, &fontSize, jni_double, 1);
        }
        break;
        case 10 : // default
        {
            if (in.size() != 1)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 1);
                return types::Function::Error;
            }

            unsigned short* defcolors = ConfigGraphicVariable::getDefaultColormap();

            int piFigurePosition[2] = {200, 200};
            int piFigureSize[2]     = {500, 500};
            int piAxesSize[2]       = {498, 366};
            int piViewPort[2]       = {0, 0};
            int piEmptyMatrix[4]    = {1, 0, 0, 0};

            // init variables
            int iZero   = 0;
            BOOL bTrue  = TRUE;
            BOOL bFalse = FALSE;
            int m       = NUMCOLORS_SCI;
            int i       = 0;
            int iCopy   = 3;
            int defaultBackground = -2;

            // reset format
            ConfigGraphicVariable::setFPF(L"");

            double* pdblColorMap = new double[m * 3];
            if (pdblColorMap == NULL)
            {
                Scierror(999, _("%s: No more memory.\n"), "xset");
                return types::Function::Error;
            }

            // Create figure if it not exist.
            int iFigureUID = getCurrentFigure();
            if (iFigureUID == 0)
            {
                iFigureUID = createNewFigureWithAxes();
                setCurrentFigure(iFigureUID);
                delete[] pdblColorMap;
                return types::Function::OK;
            }

            // Create new axes and set it in current figure
            int iSubWinUID = getCurrentSubWin();
            if (iSubWinUID != 0)
            {
                int iChildrenCount  = 0;
                int* childrencount  = &iChildrenCount;
                int* childrenUID    = 0;
                int iHidden         = 0;
                int *piHidden       = &iHidden;

                getGraphicObjectProperty(iFigureUID, __GO_CHILDREN_COUNT__, jni_int, (void **)&childrencount);
                getGraphicObjectProperty(iFigureUID, __GO_CHILDREN__, jni_string_vector, (void **)&childrenUID);

                for (i = 0; i < childrencount[0]; ++i)
                {
                    getGraphicObjectProperty(childrenUID[i], __GO_HIDDEN__, jni_bool, (void **)&piHidden);
                    if (iHidden == 0)
                    {
                        deleteGraphicObject(childrenUID[i]);
                    }
                }
            }

            cloneAxesModel(iFigureUID);

            // Set default figure properties
            setGraphicObjectProperty(iFigureUID, __GO_POSITION__, piFigurePosition, jni_int_vector, 2);
            setGraphicObjectProperty(iFigureUID, __GO_SIZE__, piFigureSize, jni_int_vector, 2);
            setGraphicObjectProperty(iFigureUID, __GO_AXES_SIZE__, piAxesSize, jni_int_vector, 2);
            setGraphicObjectProperty(iFigureUID, __GO_AUTORESIZE__, &bTrue, jni_bool, 1);
            setGraphicObjectProperty(iFigureUID, __GO_VIEWPORT__, piViewPort, jni_int_vector, 2);
            setGraphicObjectProperty(iFigureUID, __GO_NAME__, _("Figure n°%d"), jni_string, 1);
            setGraphicObjectProperty(iFigureUID, __GO_INFO_MESSAGE__, "", jni_string, 1);
            setGraphicObjectProperty(iFigureUID, __GO_PIXEL_DRAWING_MODE__, &iCopy, jni_int, 1);
            setGraphicObjectProperty(iFigureUID, __GO_ANTIALIASING__, &iZero, jni_int, 1);
            setGraphicObjectProperty(iFigureUID, __GO_IMMEDIATE_DRAWING__, &bTrue, jni_bool, 1);
            setGraphicObjectProperty(iFigureUID, __GO_BACKGROUND__, &defaultBackground, jni_int, 1);
            setGraphicObjectProperty(iFigureUID, __GO_VISIBLE__, &bTrue, jni_bool, 1);
            setGraphicObjectProperty(iFigureUID, __GO_ROTATION_TYPE__, &iZero, jni_int, 1);
            setGraphicObjectProperty(iFigureUID, __GO_EVENTHANDLER__, "", jni_string, 1);
            setGraphicObjectProperty(iFigureUID, __GO_EVENTHANDLER_ENABLE__, &bFalse, jni_bool, 1);
            setGraphicObjectProperty(iFigureUID, __GO_USER_DATA__, piEmptyMatrix, jni_int_vector, 4);
            setGraphicObjectProperty(iFigureUID, __GO_RESIZEFCN__, "", jni_string, 1);
            setGraphicObjectProperty(iFigureUID, __GO_TAG__, "", jni_string, 1);

            for (i = 0; i < m; i++)
            {
                pdblColorMap[i]         = (double)(defcolors[3 * i] / 255.0);
                pdblColorMap[i + m]     = (double)(defcolors[3 * i + 1] / 255.0);
                pdblColorMap[i + 2 * m] = (double)(defcolors[3 * i + 2] / 255.0);
            }

            setGraphicObjectProperty(iFigureUID, __GO_COLORMAP__, pdblColorMap, jni_double_vector, 3 * m);
            setGraphicObjectProperty(iFigureUID, __GO_PARENT__, "", jni_string, 1);
            delete[] pdblColorMap;
        }
        break;
        case 6 : // clipgrf
        {
            int clipState = 1;
            /* special treatement for xset("cligrf") */
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_CLIP_STATE__, &clipState, jni_int, 1);
        }
        break;
        case 4 : // clipoff
        {
            int clipState = 0;
            /* special treatement for xset("clipoff") */
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_CLIP_STATE__, &clipState, jni_int, 1);
        }
        break;
        case 16 : // hidden3d
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int hiddenColor = (int) in[1]->getAs<types::Double>()->get(0);
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_HIDDEN_COLOR__, &hiddenColor, jni_int, 1);
        }
        break;
        case 12 : // font
        {
            if (in.size() != 3)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 3);
                return types::Function::Error;
            }

            int fontStyle = (int) in[1]->getAs<types::Double>()->get(0);
            double fontSize = in[2]->getAs<types::Double>()->get(0);

            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_FONT_SIZE__, &fontSize, jni_double, 1);
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_FONT_STYLE__, &fontStyle, jni_int, 1);
        }
        break;
        case 11 : // window
        case 30 : // figure
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            // Find if window already exists, if not create a new one
            int iID = (int)in[1]->getAs<types::Double>()->get(0);
            int iFigureUID = getFigureFromIndex(iID);
            int iAxesUID = 0;
            int* piAxesUID = &iAxesUID;

            if (iFigureUID == 0)
            {
                iFigureUID = createNewFigureWithAxes();
                setGraphicObjectProperty(iFigureUID, __GO_ID__, &iID, jni_int, 1);
            }

            setCurrentFigure(iFigureUID);
            getGraphicObjectProperty(iFigureUID, __GO_SELECTED_CHILD__, jni_int, (void**)&piAxesUID);
            setCurrentSubWin(iAxesUID);
        }
        break;
        case 14 : // foreground
        case 7 : // color
        case 23 : // pattern
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int iColor = (int) in[1]->getAs<types::Double>()->get(0);
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_COLOR__, &iColor, jni_int, 1);
        }
        break;
        case 3 : // background
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int iColor = (int) in[1]->getAs<types::Double>()->get(0);
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_BACKGROUND__, &iColor, jni_int, 1);
        }
        break;
        case 25 : // thickness
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            sciSetLineWidth(getOrCreateDefaultSubwin(), (int)in[1]->getAs<types::Double>()->get(0));
        }
        break;
        case 19 : // line style
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int lineStyle = (int) in[1]->getAs<types::Double>()->get(0);
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_STYLE__, &lineStyle, jni_int, 1);
        }
        break;
        case 9 : // dashes
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int lineStyle = (int) in[1]->getAs<types::Double>()->get(0);
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_STYLE__, &lineStyle, jni_int, 1);
        }
        break;
        case 33 : // wresize
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int iAutoResizeMode = (int)in[1]->getAs<types::Double>()->get(0);
            setGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_AUTORESIZE__, &iAutoResizeMode, jni_bool, 1);
        }
        break;
        case 32 : // wpos
        {
            int figurePosition[2];
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            getOrCreateDefaultSubwin();

            figurePosition[0] = (int)in[1]->getAs<types::Double>()->get(0);
            figurePosition[1] = (int)in[1]->getAs<types::Double>()->get(1);
            setGraphicObjectProperty(getCurrentFigure(), __GO_POSITION__, figurePosition, jni_int_vector, 2);
        }
        break;
        case 31 : // wpdim
        case 28 : // wdim
        {
            int figureSize[2] = {0, 0};
            if (in.size() != 2 && in.size() != 3)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d or %d expected.\n"), "xset", 2, 3);
                return types::Function::Error;
            }

            figureSize[0] = (int)in[1]->getAs<types::Double>()->get(0);
            if (in.size() == 3)
            {
                figureSize[1] = (int)in[2]->getAs<types::Double>()->get(0);
            }

            /* Xwindows limits dimensions to 2^16 */
            if ((figureSize[0] > 65535) || (figureSize[1] > 65535))
            {
                figureSize[0] = std::min(figureSize[0], 65535);
                figureSize[1] = std::min(figureSize[1], 65535);
                if (ConfigVariable::getWarningMode())
                {
                    sciprint(_("%s: window dimensions have been set less than 2^16.\n"), "xset");
                }
            }

            getOrCreateDefaultSubwin();
            setGraphicObjectProperty(getCurrentFigure(), __GO_SIZE__, figureSize, jni_int_vector, 2);
        }
        break;
        case 27 : // viewport
        {
            if (in.size() != 3)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 3);
                return types::Function::Error;
            }

            int viewport[4] = {0, 0, 0, 0};
            viewport[0] = (int)in[1]->getAs<types::Double>()->get(0);
            viewport[1] = (int)in[2]->getAs<types::Double>()->get(0);

            getOrCreateDefaultSubwin();
            setGraphicObjectProperty(getCurrentFigure(), __GO_VIEWPORT__, viewport, jni_int_vector, 2);
        }
        break;
        case 18 : // line mode
        {
            if (in.size() != 2)
            {
                Scierror(77, _("%s: Wrong number of input arguments: %d expected.\n"), "xset", 2);
                return types::Function::Error;
            }

            int iSubwinUID = getOrCreateDefaultSubwin();
            int iZero = 0;
            int iOne = 1;

            if (in[1]->getAs<types::Double>()->get(0) == 0)
            {
                setGraphicObjectProperty(iSubwinUID, __GO_LINE_MODE__, &iZero, jni_bool, 1);
            }
            else
            {
                setGraphicObjectProperty(iSubwinUID, __GO_LINE_MODE__, &iOne, jni_bool, 1);
            }
        }
        break;
        default :
        {
            char* pstWhat = wide_string_to_UTF8(pwcsWhat);
            Scierror(999, _("%s: Unrecognized input argument: '%s'.\n"), "xset", pstWhat);
            FREE(pstWhat);
            return types::Function::Error;
        }
    }

    return types::Function::OK;
}
Example #25
0
bool getDimsFromArguments(types::typed_list& in, const std::string& _pstName, int* _iDims, int** _piDims, bool* _alloc)
{
    types::Double* pOut = 0;

    *_alloc = false;
    *_iDims = 0;
    *_piDims = NULL;

    if (in.size() == 0)
    {
        *_iDims = 2;
        *_piDims = new int[*_iDims];
        (*_piDims)[0] = 1;
        (*_piDims)[1] = 1;
        *_alloc = true;
        return true;
    }
    else if (in.size() == 1)
    {
        *_iDims = 1;
        // :
        if (in[0]->isColon())
        {
            *_iDims = -1;
            return false;
        }

        if (in[0]->isArrayOf() == false)
        {
            if (in[0]->isSparse())
            {
                types::Sparse* sp = in[0]->getAs<types::Sparse>();
                *_iDims = sp->getDims();
                *_piDims = sp->getDimsArray();
                return true;
            }
            else if (in[0]->isSparseBool())
            {
                types::SparseBool* sp = in[0]->getAs<types::SparseBool>();
                *_iDims = sp->getDims();
                *_piDims = sp->getDimsArray();
                return true;
            }
            return false;
        }

        types::GenericType* pIn = in[0]->getAs<types::GenericType>();
        *_iDims = pIn->getDims();
        *_piDims = pIn->getDimsArray();

        return true;
    }
    else
    {
        *_iDims = static_cast<int>(in.size());
        *_piDims = new int[*_iDims];
        *_alloc = true;
        for (int i = 0; i < *_iDims; i++)
        {
            if (in[i]->isArrayOf() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1);
                delete[] * _piDims;
                return false;
            }

            types::GenericType* pGTIn = in[i]->getAs<types::GenericType>();
            if (pGTIn->isScalar() == false || pGTIn->isComplex())
            {
                Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1);
                delete[] * _piDims;
                return false;
            }

            switch (in[i]->getType())
            {
                case types::InternalType::ScilabDouble:
                {
                    double dValue = in[i]->getAs<types::Double>()->get(0);
                    if (dValue >= INT_MAX)
                    {
                        Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX);
                        delete[] * _piDims;
                        return false;
                    }
                    (*_piDims)[i] = static_cast<int>(dValue);
                }
                break;
                case types::InternalType::ScilabInt8:
                    (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::Int8>()->get()[0]);
                    break;
                case types::InternalType::ScilabUInt8:
                    (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt8>()->get()[0]);
                    break;
                case types::InternalType::ScilabInt16:
                    (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::Int16>()->get()[0]);
                    break;
                case types::InternalType::ScilabUInt16:
                    (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt16>()->get()[0]);
                    break;
                case types::InternalType::ScilabInt32:
                    (*_piDims)[i] = in[i]->getAs<types::Int32>()->get()[0];
                    break;
                case types::InternalType::ScilabUInt32:
                    (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt32>()->get()[0]);
                    break;
                case types::InternalType::ScilabInt64:
                {
                    long long llValue = in[i]->getAs<types::Int64>()->get(0);
                    if (llValue >= INT_MAX)
                    {
                        Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX);
                        delete[] * _piDims;
                        return false;
                    }
                    (*_piDims)[i] = static_cast<int>(llValue);
                    break;
                }
                case types::InternalType::ScilabUInt64:
                {
                    unsigned long long ullValue = in[i]->getAs<types::UInt64>()->get(0);
                    if (ullValue >= INT_MAX)
                    {
                        Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX);
                        delete[] * _piDims;
                        return false;
                    }
                    (*_piDims)[i] = static_cast<int>(ullValue);
                    break;
                }
                default:
                    Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1);
                    delete[] * _piDims;
                    return false;
            }
        }

        return true;
    }

    return false;
}
Example #26
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_fft(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    int iDimLength          = 0;
    int iDimCount           = 0;
    int iInc                = 0;
    int iWay                = -1;
    int iSize               = 0;
    int iOne                = 1;
    int iErr                = 0;
    double dblZero          = 0;

    //workspace
    int iWS                 = 0;
    int* piWS               = NULL;
    types::Double* pIn1     = NULL;

    //check input parameters
    if (in.size() != 1 && in.size() != 2 && in.size() != 4)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d or %d expected.\n"), "fft", 1, 4);
        return types::Function::Error;
    }

    switch (in.size())
    {
        case 4 :
            //check fourth input parameter : inc
            if (in[3]->isDouble() == false || in[3]->getAs<types::Double>()->isScalar() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: Scalar expected.\n"), "fft", 4);
                return types::Function::Error;
            }

            iInc = (int)in[3]->getAs<types::Double>()->get(0);

            //check third input parameter : dim
            if (in[2]->isDouble() == false || in[2]->getAs<types::Double>()->isScalar() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: Scalar expected.\n"), "fft", 3);
                return types::Function::Error;
            }

            iDimLength = (int)in[2]->getAs<types::Double>()->get(0);
            iDimCount = 3; //any value > 2 (used as a flag)

        case 2 :
            //check third input parameter : way
            if (in[1]->isDouble() == false || in[1]->getAs<types::Double>()->isScalar() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: Scalar expected.\n"), "fft", 2);
                return types::Function::Error;
            }

            iWay = (int)in[1]->getAs<types::Double>()->get(0);
            if (iWay != -1 && iWay != 1)
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"), "fft", 2, "-1 1");
                return types::Function::Error;
            }

        case 1:
            if (in[0]->isDouble() == false)
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: Scalar expected.\n"), "fft", 1);
                return types::Function::Error;
            }

            pIn1 = in[0]->getAs<types::Double>();

            iDimCount = std::max(iDimCount, ((pIn1->getRows() == 1 || pIn1->getCols() == 1) ? 1 : 2));
            iSize = pIn1->getSize();
            break;
        default :
        {
            Scierror(77, _("%s: Wrong number of input argument(s): %d or %d expected.\n"), "fft", 1, 4);
            return types::Function::Error;
        }
    }

    types::Double* pOut = pIn1->clone()->getAs<types::Double>();
    pOut->setComplex(true);

    //alloc workspace required by dfft2
    iWS = 8 * maxfactor(iDimLength == 0 ? iSize : iDimLength) + 24;
    piWS = (int*)MALLOC(iWS * sizeof(int));
    if (piWS == NULL)
    {
        Scierror(999, _("%s : Memory allocation error.\n"), "fft");
        return types::Function::Error;
    }
    switch (iDimCount)
    {
        case 1 :
            iErr = fft_1dim(pOut->getReal(), pOut->getImg(), iSize, iWay, piWS, iWS);
            break;
        case 2 :
            iErr = fft_2dim(pOut->getReal(), pOut->getImg(), pOut->getRows(), pOut->getCols(), iWay, piWS, iWS);
            if (iErr == 1)
            {
                Scierror(999, _("%s : Memory allocation error.\n"), "fft");
                return types::Function::Error;
            }
            break;
        default :
            iErr = fft_ndim(pOut->getReal(), pOut->getImg(), iSize, iDimLength, iInc, iWay, piWS, iWS);
            break;
    }
    double *df = pOut->getImg();
    bool cplx = false;
    for (int i = 0; i < iSize; i++)
    {
        if (df[i] != 0)
        {
            cplx = true;
            break;
        }
    }
    if (cplx == false)
    {
        pOut->setComplex(false);
    }

    FREE(piWS);

    out.push_back(pOut);
    return types::Function::OK;
}
Example #27
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_mget(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    char* pstType   = os_strdup("l");//default type value : long
    int iSize       = 0;
    int iFile       = -1; //default file : last opened file
    int iErr        = 0;

    if (in.size() < 1 || in.size() > 3)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "mget", 1, 3);
        return types::Function::Error;
    }

    //check parameter 1
    if (in[0]->isDouble() == false || in[0]->getAs<types::Double>()->getSize() != 1)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A positive integer value expected.\n"), "mget", 1);
        return types::Function::Error;
    }

    types::Double* pDoubleTest = in[0]->getAs<types::Double>();
    if ((pDoubleTest->get(0) != (int)pDoubleTest->get(0)) || (pDoubleTest->get(0) < 0))
    {
        Scierror(999, _("%s: Wrong value for input argument #%d: A positive integer value expected.\n"), "mget", 1);
        return types::Function::Error;
    }

    iSize = static_cast<int>(in[0]->getAs<types::Double>()->get(0));

    if (in.size() >= 2)
    {
        //export format
        if (in[1]->isString() == false || in[1]->getAs<types::String>()->getSize() != 1)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), "mget", 2);
            return types::Function::Error;
        }

        FREE(pstType);
        pstType = wide_string_to_UTF8(in[1]->getAs<types::String>()->get(0));
    }

    if (in.size() == 3)
    {
        if (in[2]->isDouble() == false || in[2]->getAs<types::Double>()->getSize() != 1)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A integer expected.\n"), "mget", 3);
            return types::Function::Error;
        }

        iFile = static_cast<int>(in[2]->getAs<types::Double>()->get(0));
    }

    switch (iFile)
    {
        case 0: // stderr
        case 6: // stdout
            FREE(pstType);
            Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mget", iFile);
            return types::Function::Error;
    }

    types::File* pFile = FileManager::getFile(iFile);
    // file opened with fortran open function
    if (pFile == NULL || pFile->getFileType() == 1)
    {
        FREE(pstType);
        Scierror(999, _("%s: Wrong file descriptor: %d.\n"), "mget", iFile);
        return types::Function::Error;
    }


    types::Double* pD = new types::Double(1, iSize);
    double* pData = pD->get();
    C2F(mget)(&iFile, pData, &iSize, pstType, &iErr);
    FREE(pstType);
    if (iErr > 0)
    {
        return types::Function::Error;
    }

    if (iErr < 0) //no error
    {
        int iNewSize = (-iErr) - 1;
        if (iNewSize < iSize)
        {
            //read data are smaller then excepted size
            types::Double* pNewD = new types::Double(1, iNewSize);
            double* pNewData = pNewD->getReal();
            for (int i = 0 ; i < iNewSize ; i++)
            {
                pNewData[i] = pData[i];
            }

            delete pD;
            pD = pNewD;
        }
    }
    out.push_back(pD);
    return types::Function::OK;
}
Example #28
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_imag(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    if (in.size() != 1)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "imag", 1);
        return types::Function::Error;
    }

    if (_iRetCount > 1)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "imag", 1);
        return types::Function::Error;
    }

    if (in[0]->isDouble())
    {
        types::Double* pDblIn = in[0]->getAs<types::Double>();

        int iSize = pDblIn->getSize();
        int iOne = 1;

        types::Double* pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray());

        if (pDblIn->isComplex() == false)
        {
            memset(pDblOut->get(), 0x00, iSize * sizeof(double));
        }
        else
        {
            C2F(dcopy)(&iSize, pDblIn->getImg(), &iOne, pDblOut->getReal(), &iOne);
        }


        out.push_back(pDblOut);
    }
    else if (in[0]->isSparse())
    {
        types::Sparse* pSparseIn = in[0]->getAs<types::Sparse>();
        types::Sparse* pSparseOut = new types::Sparse(pSparseIn->getRows(), pSparseIn->getCols());

        if (pSparseIn->isComplex() == false)
        {
            out.push_back(pSparseOut);
            return types::Function::OK;
        }

        int const nonZeros = static_cast<int>(pSparseIn->nonZeros());
        int* pRows = new int[nonZeros * 2];
        pSparseIn->outputRowCol(pRows);
        int* pCols = pRows + nonZeros;

        for (int i = 0 ; i < nonZeros ; i++)
        {
            std::complex<double> cplx = pSparseIn->getImg(pRows[i] - 1, pCols[i] - 1);
            pSparseOut->set(pRows[i] - 1, pCols[i] - 1, cplx.imag(), false);
        }

        pSparseOut->finalize();

        delete[] pRows;

        out.push_back(pSparseOut);
    }
    else if (in[0]->isPoly())
    {
        types::Polynom* pPolyIn  = in[0]->getAs<types::Polynom>();
        types::Polynom* pPolyOut = NULL;

        if (pPolyIn->isComplex())
        {
            pPolyOut = new types::Polynom(pPolyIn->getVariableName(), pPolyIn->getDims(), pPolyIn->getDimsArray());
            for (int i = 0; i < pPolyIn->getSize(); i++)
            {
                int rank = pPolyIn->get(i)->getRank();
                int iNewRank = rank;

                // Reduce the rank of output polynom if the last ranks are null
                for (int j = rank ; j > 0 ; j--)
                {
                    if (pPolyIn->get(i)->getImg()[j] == 0.0)
                    {
                        iNewRank--;
                    }
                    else
                    {
                        break;
                    }
                }

                double* dataReal = NULL;
                types::SinglePoly* pSP = new types::SinglePoly(&dataReal, iNewRank);

                for (int j = 0; j < iNewRank + 1; j++)
                {
                    dataReal[j] = pPolyIn->get(i)->getImg()[j];
                }

                pPolyOut->set(i, pSP);
                delete pSP;
                pSP = NULL;
            }
        }
        else
        {
            int iSize = pPolyIn->getSize();
            int* piRanks = new int[iSize];
            memset(piRanks, 0x00, iSize * sizeof(int));
            pPolyOut = new types::Polynom(pPolyIn->getVariableName(), pPolyIn->getDims(), pPolyIn->getDimsArray(), piRanks);
            pPolyOut->setZeros();
            delete[] piRanks;
        }

        out.push_back(pPolyOut);
    }
    else
    {
        ast::ExecVisitor exec;
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_imag";
        return Overload::call(wstFuncName, in, _iRetCount, out, &exec);
    }

    return types::Function::OK;
}
Example #29
0
/*--------------------------------------------------------------------------*/
Function::ReturnValue sci_testAnalysis(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    if (in.size() == 0)
    {
        Scierror(999, _("%s: Wrong number of input arguments: at least %d expected.\n"), "testAnalysis", 1);
        return Function::Error;
    }

    // check that arguments are a string
    unsigned int i = 1;
    Location loc;
    ast::exps_t * args = new exps_t();
    args->reserve(in.size() - 1);
    for (const auto arg : in)
    {
        if (!arg->isString() || arg->getAs<types::String>()->getSize() != 1)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "testAnalysis", i);
            return Function::Error;
        }
        if (i > 1)
        {
            symbol::Symbol sym(arg->getAs<types::String>()->get(0));
            args->emplace_back(new ast::SimpleVar(loc, sym));
        }
        ++i;
    }

    symbol::Symbol sym(in[0]->getAs<types::String>()->get(0));
    ast::SimpleVar * var = new ast::SimpleVar(loc, sym);
    ast::CallExp ce(loc, *var, *args);

    analysis::AnalysisVisitor analysis;
    ce.accept(analysis);

    //analysis.print_info();

    analysis::TIType & t = analysis.getResult().getType();
    Struct * pOut = new Struct(1, 1);
    pOut->addField(L"type");
    pOut->get(0)->set(L"type", new String(analysis::TIType::toString(t.type).c_str()));

    pOut->addField(L"rows");
    if (t.rows.isConstant())
    {
        pOut->get(0)->set(L"rows", new Double(t.rows.getConstant()));
    }
    else
    {
        pOut->get(0)->set(L"rows", new Double(analysis::tools::NaN()));
    }

    pOut->addField(L"cols");
    if (t.cols.isConstant())
    {
        pOut->get(0)->set(L"cols", new Double(t.cols.getConstant()));
    }
    else
    {
        pOut->get(0)->set(L"cols", new Double(analysis::tools::NaN()));
    }
    out.push_back(pOut);

    //ast::DebugVisitor debugMe;
    //pExp->accept(debugMe);

    //ast::PrintVisitor printMe(std::wcout);
    //pExp->accept(printMe);

    return Function::OK;
}
Example #30
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_slint(types::typed_list & in, int _iRetCount, types::typed_list & out)
{
    slint::SLintResult * results = nullptr;
    bool printResults = false;
    const int size = (int)in.size();
    types::String * conf = nullptr;
    types::String * outFile = nullptr;

    if (size == 0 || size >= 4)
    {
        Scierror(999, _("%s: Wrong number of input arguments: at least %d expected.\n"), "slint", 1);
        return types::Function::Error;
    }

    if (!in[0]->isString())
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "slint", 1);
        return types::Function::Error;
    }

    switch (size)
    {
        case 1:
        {
            printResults = true;
            break;
        }
        case 2:
        {
            if (in[1]->isBool())
            {
                if (in[1]->getAs<types::Bool>()->getSize() == 1)
                {
                    printResults = in[1]->getAs<types::Bool>()->get(0) == 0 ? false : true;
                }
                else
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A single boolean expected.\n"), "slint", 2);
                    return types::Function::Error;
                }
            }
            else if (in[1]->isString())
            {
                outFile = in[1]->getAs<types::String>();
            }
            else
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A string or a boolean expected.\n"), "slint", 2);
                return types::Function::Error;
            }
            break;
        }
        case 3:
        {
            if (in[2]->isBool())
            {
                if (in[2]->getAs<types::Bool>()->getSize() == 1)
                {
                    printResults = in[2]->getAs<types::Bool>()->get(0) == 0 ? false : true;
                }
                else
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A single boolean expected.\n"), "slint", 3);
                    return types::Function::Error;
                }
            }
            else if (in[2]->isString())
            {
                outFile = in[2]->getAs<types::String>();
            }
            else
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A string or a boolean expected.\n"), "slint", 3);
                return types::Function::Error;
            }

            if (!in[1]->isString())
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "slint", 3);
                return types::Function::Error;
            }
            conf = in[1]->getAs<types::String>();
            break;
        }
    }

    try
    {
        slint::SLintOptions options;
        if (conf)
        {
            if (conf->getSize() == 1)
            {
                slint::XMLConfig::getOptions(conf->get(0), options);
            }
            else
            {
                slint::XMLConfig::getOptions(*conf, options);
            }
        }
        else
        {
            slint::XMLConfig::getOptions(L"SCI/modules/slint/etc/slint.xml", options);
        }

        if (outFile)
        {
            if (conf && conf->getSize() >= 2 && (std::wstring(conf->get(0)) == L"cnes"))
            {
                const slint::CNES::ToolConfiguration tc = slint::CNES::ToolConfiguration::createFromXml(conf->get(1));
                const std::wstring out(outFile->get(0));
                const std::size_t pos = out.find_last_of(L'.');
                if (pos != std::string::npos && out.substr(pos) == L".csv")
                {
                    results = new slint::CNES::CNESCsvResult(tc, conf, options.getId(), outFile->get(0));
                }
                else
                {
                    results = new slint::CNES::CNESXmlResult(tc, conf, options.getId(), outFile->get(0));
                }
            }
            else
            {
                results = new slint::SLintXmlResult(outFile->get(0));
            }
        }
        else
        {
            if (printResults)
            {
                results = new slint::SLintScilabResult();
            }
            else
            {
                results = new slint::SLintScilabOut();
            }
        }

        slint::SLint slint(options, *results);
        slint.setFiles(in[0]->getAs<types::String>());
        slint.check();
        results->finalize();

        if (!outFile && !printResults)
        {
            out.emplace_back(static_cast<slint::SLintScilabOut *>(results)->getStruct());
        }
    }
    catch (slint::PCREException & e)
    {
        delete results;
        Scierror(999, _("%s: %s\n"), "slint", e.what(), 1);
        return types::Function::Error;
    }
    catch (slint::FileException & e)
    {
        delete results;
        Scierror(999, _("%s: %s\n"), "slint", e.what(), 1);
        return types::Function::Error;
    }
    catch (slint::SLintXMLException & e)
    {
        delete results;
        Scierror(999, _("%s: %s\n"), "slint", e.what(), 1);
        return types::Function::Error;
    }

    delete results;

    if (contributionMsg)
    {
        sciprint("%s\n", _("Module developed with the contribution of CNES."));
        contributionMsg = false;
    }

    return types::Function::OK;
}