add array example

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11478 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Baozeng Ding 2009-07-30 09:41:11 +00:00
commit a19aea6b4b
16 changed files with 456 additions and 240 deletions

View file

@ -0,0 +1,17 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.c
TARGET = example
INTERFACE = example.i
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab
clean::
$(MAKE) -f $(TOP)/Makefile scilab_clean
rm -f *.sce *.so lib*lib.c *_wrap.c
check: all
$(MAKE) -f $(TOP)/Makefile scilab_run

View file

@ -0,0 +1,61 @@
/* FILE : matrix.c : some simple 4x4 matrix operations */
#include <stdlib.h>
#include <stdio.h>
double **new_matrix() {
int i;
double **M;
M = (double **) malloc(4*sizeof(double *));
M[0] = (double *) malloc(16*sizeof(double));
for (i = 0; i < 4; i++) {
M[i] = M[0] + 4*i;
}
return M;
}
void destroy_matrix(double **M) {
free(M[0]);
free(M);
}
void print_matrix(double **M) {
int i,j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%10g ", M[i][j]);
}
printf("\n");
}
}
void mat_mult(double **m1, double **m2, double **m3) {
int i,j,k;
double temp[4][4];
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
temp[i][j] = 0;
for (k = 0; k < 4; k++)
temp[i][j] += m1[i][k]*m2[k][j];
}
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
m3[i][j] = temp[i][j];
}

View file

@ -0,0 +1,36 @@
%module example
// FILE : matrix.i
%{
void set_m(double **M, int i, int j, double val) {
M[i][j] = val;
}
double get_m(double **M, int i, int j) {
return M[i][j];
}
%}
%inline {
/*** Matrix Operations ***/
extern double **new_matrix();
/* Creates a new matrix and returns a pointer to it */
extern void destroy_matrix(double **M);
/* Destroys the matrix M */
extern void print_matrix(double **M);
/* Prints out the matrix M */
extern void set_m(double **M, int i, int j, double val);
/* Sets M[i][j] = val*/
extern double get_m(double **M, int i, int j);
/* Returns M[i][j] */
extern void mat_mult(double **a, double **b, double **c);
/* Multiplies matrix a by b and places the result in c*/
}

View file

@ -0,0 +1,41 @@
// builder the *.so
exec builder.sce
// loader the *.so
exec loader.sce
// creat a new matrix
x = new_matrix();
for i = 0 : 3;
for j = 0 : 3;
set_m(x, i, j, i+j);
end;
end;
// print the matrix
print_matrix(x);
// another matrix
y = new_matrix();
for i = 0 : 3;
for j = 0 : 3;
set_m(y, i, j, i-j);
end;
end;
// print the matrix
print_matrix(y);
// mat_mult the two matrix, and the result is stored in a new matrix
z = new_matrix();
mat_mult(x, y, z);
print_matrix(z);
//destroy the matrix
destroy_matrix(x);
destroy_matrix(y);
destroy_matrix(z);
exit