Fix matrix example

This commit is contained in:
Simon Marchetto 2013-06-03 16:46:37 +02:00
commit a9f366b142
5 changed files with 48 additions and 144 deletions

View file

@ -4,12 +4,12 @@ SRCS = matrixlib.c
TARGET = matrixlib
INTERFACE = matrixlib.i
all::
all:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab
clean::
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean
rm -f *.sce *.so lib*lib.c *_wrap.c

View file

@ -1,32 +1,39 @@
#include <stdlib.h>
double sumitems(double *first, int nbRow, int nbCol) {
double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol)
{
int i;
double total;
for (i=0; i<(nbRow*nbCol); i++) {
total+=first[i];
double total = 0.0;
for (i=0; i<nbRow*nbCol; i++)
{
total += inputMatrix[i];
}
return total;
}
void sumitems_argoutput(double *first, int nbRow, int nbCol,double** result,int* nbrowres,int* nbcolsres) {
void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double** resultMatrix, int* nbRowRes, int* nbColRes)
{
int i;
*nbrowres=nbRow;
*nbcolsres=nbCol;
*result=malloc(nbRow*nbCol*sizeof(double));
for (i=0; i<(nbRow*nbCol); i++) {
(*result)[i]=first[i]+first[i];
int size = nbRow * nbCol;
*nbRowRes = nbRow;
*nbColRes = nbCol;
*resultMatrix = (double*) malloc(size * sizeof(double));
for (i=0; i<size; i++)
{
(*resultMatrix)[i] = inputMatrix[i] * inputMatrix[i];
}
return;
}
double* getValues(int *numberOfRow, int *numberOfCol) {
double *tempMatrix ;
void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
{
int i;
*numberOfRow=23; *numberOfCol=3;
tempMatrix= (double*)malloc(sizeof(double )* *numberOfRow * *numberOfCol);
for (i=0; i<((*numberOfRow)*(*numberOfCol)); i++) {
tempMatrix[i]=i*2;
int size;
*nbRowRes = 5;
*nbColRes = 3;
size = (*nbRowRes) * (*nbColRes);
*resultMatrix = (double*) malloc(size * sizeof(double));
for (i=0; i<size; i++)
{
(*resultMatrix)[i] = i*2;
}
return tempMatrix;
}

View file

@ -1,42 +1,15 @@
%module matrixlib
%include matrix.i
%apply (double *matrixAsInput, int rows, int cols) { (double *inputMatrix, int nbRow, int nbCol) }
%apply (double **matrixAsOutput, int *rows, int *cols) { (double **resultMatrix, int *nbRowRes, int *nbColRes) }
%apply (double* matrixAsInput,int rows,int cols){(double *first, int nbRow, int nbCol)}
%apply (double** matrixAsArgOutput,int* rows,int* cols){(double **result,int* nbRowOut,int* nbColOut)}
%typemap(out) (double*)(int *nRow, int *nCol)
%inline
{
SciErr sciErr;
sciErr = createMatrixOfDouble(pvApiCtx, Rhs+$result, *nRow, *nCol, (double *)$1);
if (sciErr.iErr) {
printError(&sciErr, 0);
return 0;
}
AssignOutputVariable(pvApiCtx, outputPosition) = Rhs+$result;
free($1);
}
%typemap (in,numinputs=0) (int *numberOfRow, int *numberOfCol)
{
}
%typemap(arginit) (int *numberOfRow, int *numberOfCol)
{
$1 =(int*)malloc(sizeof(int));
$2 =(int*)malloc(sizeof(int));
nRow =$1;
nCol =$2;
}
%typemap(freearg) (int *numberOfRow, int *numberOfCol)
{
free($1);
free($2);
}
%inline {
extern void sumitems_argoutput(double *first, int nbRow, int nbCol,double **result,int* nbRowOut,int* nbColOut);
extern double* getValues(int *numberOfRow, int *numberOfCol);
extern double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol);
extern void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double **resultMatrix, int *nbRowRes, int *nbColRes);
extern void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes);
}

View file

@ -1,9 +1,14 @@
// loader the *.so
exec loader.sce
myMatrix=[ 103 3 1 12;0 0 2043 1];
m=sumitems_argoutput(myMatrix)
myOtherMatrix=getValues();
size(myOtherMatrix)
disp(myOtherMatrix);
exit
disp("Call lib function getMatrix()");
matrix1 = getMatrix();
disp(matrix1);
disp("Call lib function sumMatrixElements()");
s = sumMatrixElements(matrix1);
disp(s);
disp("Call lib function squareMatrixElements()");
squareMatrix = squareMatrixElements(matrix1);
disp(squareMatrix);

View file

@ -1,81 +0,0 @@
#include "api_scilab.h"
double sumitems(double *first, int nbRow, int nbCol);
double* getValues(int *numberOfRow, int *numberOfCol);
int sci_sumitems(char *fname,unsigned long fname_len)
{
int iRows = 0;
int iCols = 0;
int *piAddr = NULL;
double* pdblReal = NULL;
CheckInputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 1);
SciErr sciErr;
//get variable address of the first input argument
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal);
double plop = sumitems(pdblReal, iRows, iCols);
/*
* Here, it is a scalar but it could be also a matrix... don't assume it
* it will be always 1x1
*/
int iRowsReturn=1;
int iColReturn=1;
sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, iRowsReturn, iColReturn, &plop);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
return 0;
}
int sci_getValues(char *fname,unsigned long fname_len)
{
int iRows = 0;
int iCols = 0;
int *piAddr = NULL;
double* pdblReal = NULL;
CheckInputArgument(pvApiCtx, 0, 0);
CheckOutputArgument(pvApiCtx, 1, 1);
SciErr sciErr;
int numberRow, numberCol, i;
double * matrix = getValues(&numberRow, &numberCol);
sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, numberRow, numberCol, matrix);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
return 0;
}