/*--------------------------------------------------------------------------*/ int isNamedColumnVector(void *_pvCtx, const char *_pstName) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iRows = 0; int iCols = 0; if (isNamedVarMatrixType(_pvCtx, _pstName) == 0) { return 0; } sciErr = getNamedVarDimension(_pvCtx, _pstName, &iRows, &iCols); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_IS_NAMED_COLUMN_VECTOR, _("%s: Unable to get argument dimension"), "isNamedColumnVector"); printError(&sciErr, 0); return 0; } if (iCols == 1 && iRows > 1) { return 1; } return 0; }
/*--------------------------------------------------------------------------*/ int checkNamedVarDimension(void *_pvCtx, const char *_pstName, int _iRows, int _iCols) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iRows = 0; int iCols = 0; if (isNamedVarMatrixType(_pvCtx, _pstName) == 0) { return 0; } sciErr = getNamedVarDimension(_pvCtx, _pstName, &iRows, &iCols); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CHECK_NAMED_VAR_DIMENSION, _("%s: Unable to get argument dimension"), "checkNamedVarDimension"); printError(&sciErr, 0); return 0; } if ((_iRows == iRows || _iRows == -1) && (_iCols == iCols || _iCols == -1)) { return 1; } return 0; }
/*--------------------------------------------------------------------------*/ int isNamedScalar(void *_pvCtx, const char *_pstName) { int iRows = 0; int iCols = 0; if (isNamedVarMatrixType(_pvCtx, _pstName) == 0) { return 0; } SciErr sciErr = getNamedVarDimension(_pvCtx, _pstName, &iRows, &iCols); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_IS_NAMED_SCALAR, _("%s: Unable to get argument dimension"), "isNamedScalar"); printError(&sciErr, 0); return 0; } if (iCols == 1 && iRows == 1) { return 1; } return 0; }
/*------------------------------------------------------------*/ static int first_example(void) { void* pvApiCtx = NULL; static double A[] = {1, 2, 3, 4}; int mA = 2, nA = 2; static double b[] = {4, 5}; int mb = 2, nb = 1; printf("\nExample 1:\n"); printf("Some simple computations\n"); /* Create Scilab matrices A and b */ createNamedMatrixOfDouble(pvApiCtx, "A", mA, mA, A); createNamedMatrixOfDouble(pvApiCtx, "b", mb, nb, b); SendScilabJob("disp('A=');"); SendScilabJob("disp(A);"); SendScilabJob("disp('b=');"); SendScilabJob("disp(b);"); SendScilabJob("disp('x=A\\b');"); if ( SendScilabJob("A,b,x=A\\b;") != 0) { fprintf(stdout, "Error occurred during scilab execution (SendScilabJob)\n"); } else { double *cxtmp = NULL; int m, n, lp, i; /* Get m and n */ getNamedVarDimension(pvApiCtx, "x", &m, &n); cxtmp = (double*)malloc((m * n) * sizeof(double)); readNamedMatrixOfDouble(pvApiCtx, "x", &m, &n, cxtmp); for (i = 0; i < m * n; i++) { fprintf(stdout, "x[%d] = %5.2f\n", i, cxtmp[i]); } if (cxtmp) { free(cxtmp); cxtmp = NULL; } } return 0; }
/*! public native boolean GetElement(int indr, int indc); */ JNIEXPORT jboolean JNICALL Java_javasci_SciBooleanArray_GetElement(JNIEnv *env , jobject obj_this,jint indrarg, jint indcarg) { SciErr sciErr; jboolean Value = JNI_FALSE; int *CX = NULL; jclass class_Mine = (*env)->GetObjectClass(env, obj_this); jfieldID id_name = (*env)->GetFieldID(env, class_Mine, "name","Ljava/lang/String;"); jfieldID id_m = (*env)->GetFieldID(env, class_Mine, "m", "I"); jfieldID id_n = (*env)->GetFieldID(env, class_Mine, "n", "I"); jstring jname = (jstring) (*env)->GetObjectField(env, obj_this, id_name); jint jm = (*env)->GetIntField(env, obj_this, id_m); jint jn = (*env)->GetIntField(env, obj_this, id_n); const char *cname = (*env)->GetStringUTFChars(env, jname, NULL); jfieldID id_x; jbooleanArray jx; jboolean *cx = NULL; int dimension[2]; int i = 0; int cm = 0, cn = 0; sciErr = getNamedVarDimension(pvApiCtx, (char*)cname, &dimension[0], &dimension[1]); if(sciErr.iErr) { fprintf(stderr,"%s", getErrorMessage(sciErr)); (*env)->ReleaseStringUTFChars(env, jname , cname); fprintf(stderr,"Error in Java_javasci_SciBooleanArray_GetElement (1).\n"); return Value; } if (dimension[0] != jm) { fprintf(stderr,"Error in Java_javasci_SciBooleanArray_GetElement (2).\n"); (*env)->ReleaseStringUTFChars(env, jname , cname); return Value; } if (dimension[1] != jn) { fprintf(stderr,"Error in Java_javasci_SciBooleanArray_GetElement (3).\n"); (*env)->ReleaseStringUTFChars(env, jname , cname); return Value; } id_x = (*env)->GetFieldID(env, class_Mine, "x", "[Z"); jx = (*env)->GetObjectField(env, obj_this, id_x); cx = (*env)->GetBooleanArrayElements(env, jx, NULL); CX = (int*) MALLOC(sizeof(int)*(jm * jn)); if (CX == NULL) { fprintf(stderr,"Error in Java_javasci_SciBooleanArray_GetElement (4).\n"); (*env)->ReleaseBooleanArrayElements(env, jx, cx, 0); return 1; } sciErr = readNamedMatrixOfBoolean(pvApiCtx, (char*)cname, &cm, &cn, CX); if(sciErr.iErr) { fprintf(stderr,"%s", getErrorMessage(sciErr)); fprintf(stderr,"Error in Java_javasci_SciBooleanArray_GetElement (5).\n"); (*env)->ReleaseBooleanArrayElements(env, jx, cx, 0); return 1; } for(i = 0; i < cm*cn; i++) { cx[i] = (jboolean)CX[i]; } FREE(CX); CX = NULL; if ( (indrarg <= 0) || (indcarg <= 0) ) { (*env)->ReleaseBooleanArrayElements(env, jx, cx, 0); (*env)->ReleaseStringUTFChars(env, jname , cname); fprintf(stderr,"Error with int indr & int indc must be >0.\n"); return Value; } if ( (indrarg > jm) || (indcarg > jn) ) { (*env)->ReleaseBooleanArrayElements(env, jx, cx, 0); (*env)->ReleaseStringUTFChars(env, jname , cname); fprintf(stderr,"Error with int indr & int indc.\n"); return Value; } Value = (jboolean)cx[(indcarg - 1) * cm + (indrarg - 1)]; (*env)->ReleaseBooleanArrayElements(env, jx, cx, 0); (*env)->ReleaseStringUTFChars(env, jname , cname); return Value; }
/*! public native int GetElement(int indr, int indc); */ JNIEXPORT jint JNICALL Java_javasci_SciIntegerArray_GetElement(JNIEnv *env , jobject obj_this,jint indrarg, jint indcarg) { SciErr sciErr; int Value = 0; int j = 0; jclass class_Mine = (*env)->GetObjectClass(env, obj_this); jfieldID id_name = (*env)->GetFieldID(env, class_Mine, "name","Ljava/lang/String;"); jfieldID id_m = (*env)->GetFieldID(env, class_Mine, "m", "I"); jfieldID id_n = (*env)->GetFieldID(env, class_Mine, "n", "I"); jstring jname = (jstring) (*env)->GetObjectField(env, obj_this, id_name); jint jm = (*env)->GetIntField(env, obj_this, id_m); jint jn = (*env)->GetIntField(env, obj_this, id_n); const char *cname = (*env)->GetStringUTFChars(env, jname, NULL); jfieldID id_x; jintArray jx; jint *cx = NULL; int *icx = NULL; int dimension[2]; int cm = 0, cn = 0; sciErr = getNamedVarDimension(pvApiCtx, (char*)cname, &dimension[0], &dimension[1]); if(sciErr.iErr) { fprintf(stderr,"%s", getErrorMessage(sciErr)); (*env)->ReleaseStringUTFChars(env, jname , cname); fprintf(stderr,"Error in Java_javasci_SciIntegerArray_GetElement (1).\n"); return Value; } if (dimension[0] != jm) { fprintf(stderr,"Error in Java_javasci_SciIntegerArray_GetElement (2).\n"); (*env)->ReleaseStringUTFChars(env, jname , cname); return Value; } if (dimension[1] != jn) { fprintf(stderr,"Error in Java_javasci_SciIntegerArray_GetElement (3).\n"); (*env)->ReleaseStringUTFChars(env, jname , cname); return Value; } id_x = (*env)->GetFieldID(env, class_Mine, "x", "[D"); jx = (*env)->GetObjectField(env, obj_this, id_x); cx = (*env)->GetIntArrayElements(env, jx, NULL); icx = (int*)MALLOC(sizeof(int) * (cm * cn)); if (icx == NULL) { fprintf(stderr,"Error in Java_javasci_SciIntegerArray_GetElement (4).\n"); (*env)->ReleaseIntArrayElements(env, jx, cx, 0); (*env)->ReleaseStringUTFChars(env, jname , cname); return Value; } for (j = 0; j < cm * cn; j++) icx[j] = (int)cx[j]; sciErr = readNamedMatrixOfInteger32(pvApiCtx, (char*)cname, &cm, &cn, icx); if(sciErr.iErr) { fprintf(stderr,"%s", getErrorMessage(sciErr)); FREE(icx); icx = NULL; fprintf(stderr,"Error in Java_javasci_SciIntegerArray_GetElement (5).\n"); (*env)->ReleaseIntArrayElements(env, jx, cx, 0); (*env)->ReleaseStringUTFChars(env, jname , cname); return Value; } FREE(icx); icx = NULL; if ( (indrarg <= 0) || (indcarg <= 0) ) { (*env)->ReleaseIntArrayElements(env, jx, cx, 0); (*env)->ReleaseStringUTFChars(env, jname , cname); fprintf(stderr,"Error with int indr & int indc must be >0.\n"); return Value; } if ( (indrarg > jm) || (indcarg > jn) ) { (*env)->ReleaseIntArrayElements(env, jx, cx, 0); (*env)->ReleaseStringUTFChars(env, jname , cname); fprintf(stderr,"Error with int indr & int indc.\n"); return Value; } Value = cx[(indcarg - 1) * cm + (indrarg - 1)]; (*env)->ReleaseIntArrayElements(env, jx, cx, 0); (*env)->ReleaseStringUTFChars(env, jname , cname); return Value; }
/*--------------------------------------------------------------------------*/ int SendScilabJob(char *job) { SciErr sciErr; int retCode = -1; char *command = NULL; #define COMMAND_EXECSTR "Err_Job = execstr(TMP_EXEC_STRING,\"errcatch\",\"n\");quit;" #define COMMAND_CLEAR "clear TMP_EXEC_STRING;clear Err_Job;quit;" if (getCallScilabEngineState() == CALL_SCILAB_ENGINE_STOP) { fprintf(stderr, "Error: SendScilabJob call_scilab engine not started.\n"); return retCode; } command = strdup(job); if (command) { double Err_Job = 0.; int m = 0, n = 0; /* clear prev. Err , TMP_EXEC_STRING scilab variables */ C2F(scirun) (COMMAND_CLEAR, (long int)strlen(COMMAND_CLEAR)); SetLastJob(command); /* Creation of a temp variable in Scilab which contains the command */ sciErr = createNamedMatrixOfString(pvApiCtx, "TMP_EXEC_STRING", 1, 1, &command); if (sciErr.iErr) { printError(&sciErr, 0); /* Problem */ fprintf(stderr, "Error: SendScilabJob (1) call_scilab failed to create the temporary variable 'TMP_EXEC_STRING'.\n"); retCode = -1; if (command) { FREE(command); command = NULL; } return retCode; } /* Run the command within an execstr */ C2F(scirun) (COMMAND_EXECSTR, (long int)strlen(COMMAND_EXECSTR)); sciErr = getNamedVarDimension(pvApiCtx, "Err_Job", &m, &n); if (sciErr.iErr) { printError(&sciErr, 0); fprintf(stderr, "Error: SendScilabJob (2) call_scilab failed to detect the temporary variable 'Err_Job' size.\n"); retCode = -2; if (command) { FREE(command); command = NULL; } return retCode; } if ((m != 1) && (n != 1)) { fprintf(stderr, "Error: SendScilabJob (3) call_scilab detected a badly formated 'Err_Job' variable. Size [1,1] expected.\n"); retCode = -3; if (command) { FREE(command); command = NULL; } return retCode; } sciErr = readNamedMatrixOfDouble(pvApiCtx, "Err_Job", &m, &n, &Err_Job); if (sciErr.iErr) { printError(&sciErr, 0); fprintf(stderr, "Error: SendScilabJob (4) call_scilab failed to read the temporary variable 'Err_Job'.\n"); retCode = -4; if (command) { FREE(command); command = NULL; } return retCode; } if (command) { FREE(command); command = NULL; } retCode = (int)Err_Job; /* clear prev. Err , TMP_EXEC_STRING scilab variables */ C2F(scirun) (COMMAND_CLEAR, (long int)strlen(COMMAND_CLEAR)); } else { fprintf(stderr, "Error: SendScilabJob (5) call_scilab failed to create the 'command' variable (MALLOC).\n"); retCode = -4; } return retCode; }