Fix matrix example
This commit is contained in:
parent
cf88d6a109
commit
a9f366b142
5 changed files with 48 additions and 144 deletions
|
|
@ -4,12 +4,12 @@ SRCS = matrixlib.c
|
||||||
TARGET = matrixlib
|
TARGET = matrixlib
|
||||||
INTERFACE = matrixlib.i
|
INTERFACE = matrixlib.i
|
||||||
|
|
||||||
all::
|
all:
|
||||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab
|
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab
|
||||||
|
|
||||||
|
|
||||||
clean::
|
|
||||||
|
clean:
|
||||||
$(MAKE) -f $(TOP)/Makefile scilab_clean
|
$(MAKE) -f $(TOP)/Makefile scilab_clean
|
||||||
rm -f *.sce *.so lib*lib.c *_wrap.c
|
rm -f *.sce *.so lib*lib.c *_wrap.c
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,39 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
double sumitems(double *first, int nbRow, int nbCol) {
|
|
||||||
|
double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
double total;
|
double total = 0.0;
|
||||||
for (i=0; i<(nbRow*nbCol); i++) {
|
for (i=0; i<nbRow*nbCol; i++)
|
||||||
total+=first[i];
|
{
|
||||||
|
total += inputMatrix[i];
|
||||||
}
|
}
|
||||||
return total;
|
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;
|
int i;
|
||||||
*nbrowres=nbRow;
|
int size = nbRow * nbCol;
|
||||||
*nbcolsres=nbCol;
|
*nbRowRes = nbRow;
|
||||||
*result=malloc(nbRow*nbCol*sizeof(double));
|
*nbColRes = nbCol;
|
||||||
for (i=0; i<(nbRow*nbCol); i++) {
|
*resultMatrix = (double*) malloc(size * sizeof(double));
|
||||||
(*result)[i]=first[i]+first[i];
|
for (i=0; i<size; i++)
|
||||||
|
{
|
||||||
|
(*resultMatrix)[i] = inputMatrix[i] * inputMatrix[i];
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double* getValues(int *numberOfRow, int *numberOfCol) {
|
void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
|
||||||
double *tempMatrix ;
|
{
|
||||||
int i;
|
int i;
|
||||||
*numberOfRow=23; *numberOfCol=3;
|
int size;
|
||||||
tempMatrix= (double*)malloc(sizeof(double )* *numberOfRow * *numberOfCol);
|
*nbRowRes = 5;
|
||||||
|
*nbColRes = 3;
|
||||||
for (i=0; i<((*numberOfRow)*(*numberOfCol)); i++) {
|
size = (*nbRowRes) * (*nbColRes);
|
||||||
tempMatrix[i]=i*2;
|
*resultMatrix = (double*) malloc(size * sizeof(double));
|
||||||
|
for (i=0; i<size; i++)
|
||||||
|
{
|
||||||
|
(*resultMatrix)[i] = i*2;
|
||||||
}
|
}
|
||||||
return tempMatrix;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,15 @@
|
||||||
%module matrixlib
|
%module matrixlib
|
||||||
|
|
||||||
%include matrix.i
|
%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;
|
extern double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol);
|
||||||
sciErr = createMatrixOfDouble(pvApiCtx, Rhs+$result, *nRow, *nCol, (double *)$1);
|
extern void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double **resultMatrix, int *nbRowRes, int *nbColRes);
|
||||||
if (sciErr.iErr) {
|
extern void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,14 @@
|
||||||
// loader the *.so
|
|
||||||
exec loader.sce
|
exec loader.sce
|
||||||
|
|
||||||
myMatrix=[ 103 3 1 12;0 0 2043 1];
|
disp("Call lib function getMatrix()");
|
||||||
m=sumitems_argoutput(myMatrix)
|
matrix1 = getMatrix();
|
||||||
myOtherMatrix=getValues();
|
disp(matrix1);
|
||||||
size(myOtherMatrix)
|
|
||||||
disp(myOtherMatrix);
|
disp("Call lib function sumMatrixElements()");
|
||||||
exit
|
s = sumMatrixElements(matrix1);
|
||||||
|
disp(s);
|
||||||
|
|
||||||
|
disp("Call lib function squareMatrixElements()");
|
||||||
|
squareMatrix = squareMatrixElements(matrix1);
|
||||||
|
disp(squareMatrix);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue