Scilab: update matrix2 example (with int & string matrix conversion)
This commit is contained in:
parent
e6af8948ef
commit
8ca085b528
3 changed files with 135 additions and 19 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol)
|
// Double matrix functions
|
||||||
|
|
||||||
|
double sumDoubleMatrix(double *inputMatrix, int nbRow, int nbCol)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
double total = 0.0;
|
double total = 0.0;
|
||||||
|
|
@ -11,7 +13,7 @@ double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol)
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double** resultMatrix, int* nbRowRes, int* nbColRes)
|
void squareDoubleMatrix(double *inputMatrix, int nbRow, int nbCol, double** resultMatrix, int* nbRowRes, int* nbColRes)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int size = nbRow * nbCol;
|
int size = nbRow * nbCol;
|
||||||
|
|
@ -24,7 +26,7 @@ void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double** re
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
|
void getDoubleMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int size;
|
int size;
|
||||||
|
|
@ -37,3 +39,77 @@ void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes)
|
||||||
(*resultMatrix)[i] = i*2;
|
(*resultMatrix)[i] = i*2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Integer matrix functions
|
||||||
|
|
||||||
|
int sumIntegerMatrix(int *inputMatrix, int nbRow, int nbCol)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int total = 0;
|
||||||
|
for (i=0; i<nbRow*nbCol; i++)
|
||||||
|
{
|
||||||
|
total += inputMatrix[i];
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
void squareIntegerMatrix(int *inputMatrix, int nbRow, int nbCol, int** resultMatrix, int* nbRowRes, int* nbColRes)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int size = nbRow * nbCol;
|
||||||
|
*nbRowRes = nbRow;
|
||||||
|
*nbColRes = nbCol;
|
||||||
|
*resultMatrix = (int*) malloc(size * sizeof(int));
|
||||||
|
for (i=0; i<size; i++)
|
||||||
|
{
|
||||||
|
(*resultMatrix)[i] = inputMatrix[i] * inputMatrix[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void getIntegerMatrix(int **resultMatrix, int *nbRowRes, int *nbColRes)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int size;
|
||||||
|
*nbRowRes = 5;
|
||||||
|
*nbColRes = 3;
|
||||||
|
size = (*nbRowRes) * (*nbColRes);
|
||||||
|
*resultMatrix = (int*) malloc(size * sizeof(int));
|
||||||
|
for (i=0; i<size; i++)
|
||||||
|
{
|
||||||
|
(*resultMatrix)[i] = i*2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// String matrix functions
|
||||||
|
|
||||||
|
char* concatStringVector(char **inputVector, int size)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int resultSize;
|
||||||
|
char *result;
|
||||||
|
resultSize = 3 * size + 1;
|
||||||
|
result = calloc(resultSize, sizeof(char));
|
||||||
|
strcpy(result, inputVector[0]);
|
||||||
|
for (i=1; i<size; i++)
|
||||||
|
{
|
||||||
|
strcat(result, " ");
|
||||||
|
strcat(result, (const char*) inputVector[i]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void getStringVector(char ***resultVector, int *sizeRes)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
*sizeRes = 12;
|
||||||
|
*resultVector = (char**) malloc((*sizeRes) * sizeof(char*));
|
||||||
|
for (i=0; i<*sizeRes; i++)
|
||||||
|
{
|
||||||
|
char* pc = (char*) calloc(3, sizeof(char));
|
||||||
|
sprintf(pc, "%d", i);
|
||||||
|
(*resultVector)[i] = pc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,25 @@
|
||||||
|
|
||||||
%include matrix.i
|
%include matrix.i
|
||||||
|
|
||||||
%apply (double *matrixAsInput, int rows, int cols) { (double *inputMatrix, int nbRow, int nbCol) }
|
%apply (double *matrixIn, int matrixInRowCount, int matrixInColCount) { (double *inputMatrix, int nbRow, int nbCol) }
|
||||||
%apply (double **matrixAsOutput, int *rows, int *cols) { (double **resultMatrix, int *nbRowRes, int *nbColRes) }
|
%apply (double **matrixOut, int *matrixOutRowCount, int *matrixOutColCount) { (double **resultMatrix, int *nbRowRes, int *nbColRes) }
|
||||||
|
|
||||||
|
%apply (int *matrixIn, int matrixInRowCount, int matrixInColCount) { (int *inputMatrix, int nbRow, int nbCol) }
|
||||||
|
%apply (int **matrixOut, int *matrixOutRowCount, int *matrixOutColCount) { (int **resultMatrix, int *nbRowRes, int *nbColRes) }
|
||||||
|
|
||||||
%inline
|
%apply (char **vectorIn, int vectorInSize) { (char **inputVector, int size) }
|
||||||
{
|
%apply (char ***vectorOut, int *vectorOutSize) { (char ***resultVector, int *sizeRes) }
|
||||||
extern double sumMatrixElements(double *inputMatrix, int nbRow, int nbCol);
|
|
||||||
extern void squareMatrixElements(double *inputMatrix, int nbRow, int nbCol, double **resultMatrix, int *nbRowRes, int *nbColRes);
|
%inline %{
|
||||||
extern void getMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes);
|
extern double sumDoubleMatrix(double *inputMatrix, int nbRow, int nbCol);
|
||||||
}
|
extern void squareDoubleMatrix(double *inputMatrix, int nbRow, int nbCol, double **resultMatrix, int *nbRowRes, int *nbColRes);
|
||||||
|
extern void getDoubleMatrix(double **resultMatrix, int *nbRowRes, int *nbColRes);
|
||||||
|
|
||||||
|
extern int sumIntegerMatrix(int *inputMatrix, int nbRow, int nbCol);
|
||||||
|
extern void squareIntegerMatrix(int *inputMatrix, int nbRow, int nbCol, int **resultMatrix, int *nbRowRes, int *nbColRes);
|
||||||
|
extern void getIntegerMatrix(int **resultMatrix, int *nbRowRes, int *nbColRes);
|
||||||
|
|
||||||
|
extern char* concatStringVector(char **inputVector, int size);
|
||||||
|
extern void getStringVector(char ***resultVector, int *sizeRes);
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,43 @@
|
||||||
exec loader.sce
|
exec loader.sce
|
||||||
|
|
||||||
disp("Call lib function getMatrix()");
|
// Test lib double matrix functions
|
||||||
matrix1 = getMatrix();
|
disp("Call lib function getDoubleMatrix()");
|
||||||
disp(matrix1);
|
doubleMatrix = getDoubleMatrix();
|
||||||
|
disp(doubleMatrix);
|
||||||
|
|
||||||
disp("Call lib function sumMatrixElements()");
|
disp("Call lib function sumDoubleMatrix()");
|
||||||
s = sumMatrixElements(matrix1);
|
s = sumDoubleMatrix(doubleMatrix);
|
||||||
disp(s);
|
disp(s);
|
||||||
|
|
||||||
disp("Call lib function squareMatrixElements()");
|
disp("Call lib function squareDoubleMatrix()");
|
||||||
squareMatrix = squareMatrixElements(matrix1);
|
squareDoubleMatrix = squareDoubleMatrix(doubleMatrix);
|
||||||
disp(squareMatrix);
|
disp(squareDoubleMatrix);
|
||||||
|
|
||||||
|
|
||||||
|
// Test lib integer matrix functions
|
||||||
|
|
||||||
|
disp("Call lib function getIntegerMatrix()");
|
||||||
|
integerMatrix = getIntegerMatrix();
|
||||||
|
disp(integerMatrix);
|
||||||
|
|
||||||
|
disp("Call lib function sumIntegerMatrix()");
|
||||||
|
s = sumIntegerMatrix(integerMatrix);
|
||||||
|
disp(s);
|
||||||
|
|
||||||
|
disp("Call lib function squareIntegerMatrix()");
|
||||||
|
squareIntegerMatrix = squareIntegerMatrix(integerMatrix);
|
||||||
|
disp(squareIntegerMatrix);
|
||||||
|
|
||||||
|
|
||||||
|
// Test lib string matrix functions
|
||||||
|
|
||||||
|
disp("Call lib function getStringVector()");
|
||||||
|
stringVector = getStringVector();
|
||||||
|
disp(stringVector);
|
||||||
|
|
||||||
|
disp("Call lib function concatStringVector()");
|
||||||
|
stringVector2 = concatStringVector(stringVector);
|
||||||
|
disp(stringVector2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue