Initial revision.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@366 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Thien-Thi Nguyen 2000-04-03 14:55:20 +00:00
commit 6ca566dee0
12 changed files with 472 additions and 0 deletions

4
Examples/guile/README Normal file
View file

@ -0,0 +1,4 @@
This directory contains examples for Guile.
simple - The simple example from the user manual.
matrix - A very simple Matrix example.

View file

@ -0,0 +1,19 @@
CC = gcc
SRCS = matrix.c vector.c
TARGET = matrix
IFILE = package.i
MKDIR = ..
all::
$(MAKE) -f $(MKDIR)/Makefile \
SRCS='$(SRCS)' \
TARGET=$(TARGET) \
IFILE=$(IFILE) \
CC=$(CC) \
MODULE=$(MODULE) \
sub-all
clean::
rm -f matrix *_wrap* *~ .~* core

View file

@ -0,0 +1,13 @@
Matrix example. To run the example, execute the program 'matrix' and
type the following :
(load 'matrix.scm)
(do-test 0)
Alternatively, use the command-line:
./matrix -e do-test -s matrix.scm
Or, if your operating system is spiffy enough:
./matrix.scm

View file

@ -0,0 +1,24 @@
#include <tcl.h>
extern int matrix_init(Tcl_Interp *); /* Init function from matrix.i */
int main() {
int code;
char input[1024];
Tcl_Interp *interp;
interp = Tcl_CreateInterp();
/* Initialize the wrappers */
if (matrix_init(interp) == TCL_ERROR)
exit(0);
fprintf(stdout,"matrix > ");
while(fgets(input, 1024, stdin) != NULL) {
code = Tcl_Eval(interp, input);
fprintf(stdout,"%s\n",interp->result);
fprintf(stdout,"matrix > ");
}
}

View file

@ -0,0 +1,59 @@
/* FILE : matrix.c : some simple 4x4 matrix operations */
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,10 @@
#include <math.h>
typedef struct {
double x;
double y;
double z;
double w;
} Vector;

View file

@ -0,0 +1,38 @@
//
// FILE : matrix.i
%init Matrix_init
%{
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];
}
%}
%section "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*/

209
Examples/guile/matrix/matrix.scm Executable file
View file

@ -0,0 +1,209 @@
#!./matrix \
-e do-test -s
!#
;;; Authors: David Beazley <beazley@cs.uchicago.edu>, 1999
;;; Martin Froehlich <MartinFroehlich@ACM.org>, 2000
;;;
;;; PURPOSE OF THIS FILE: This file is an example for how to use the guile
;;; scripting options with a little more than trivial script. Example
;;; derived from David Beazley's matrix evaluation example. David
;;; Beazley's annotation: >>Guile script for testing out matrix
;;; operations. Disclaimer : I'm not a very good scheme
;;; programmer<<. Martin Froehlich's annotation: >>I'm not a very good
;;; scheme programmer, too<<.
;;;
;;; Explanation: The three lines at the beginning of this script are
;;; telling the kernel to load the enhanced guile interpreter named
;;; "matrix"; to execute the function "do-test" (-e option) after loading
;;; this script (-s option). There are a lot more options wich allow for
;;; even finer tuning. SEE ALSO: Section "Guile Scripts" in the "Guile
;;; reference manual -- Part I: Preliminaries".
;;;
;;;
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;;; Create a zero matrix
(define (zero M)
(define (zero-loop M i j)
(if (< i 4)
(if (< j 4) (begin
(set-m M i j 0.0)
(zero-loop M i (+ j 1)))
(zero-loop M (+ i 1) 0))))
(zero-loop M 0 0))
;;; Create an identity matrix
(define (identity M)
(define (iloop M i)
(if (< i 4) (begin
(set-m M i i 1.0)
(iloop M (+ i 1)))))
(zero M)
(iloop M 0))
;;; Rotate around x axis
(define (rotx M r)
(define temp (new-matrix))
(define rd (/ (* r 3.14159) 180.0))
(zero temp)
(set-m temp 0 0 1.0)
(set-m temp 1 1 (cos rd))
(set-m temp 1 2 (- 0 (sin rd)))
(set-m temp 2 1 (sin rd))
(set-m temp 2 2 (cos rd))
(set-m temp 3 3 1.0)
(mat-mult M temp M)
(destroy-matrix temp))
;;; Rotate around y axis
(define (roty M r)
(define temp (new-matrix))
(define rd (/ (* r 3.14159) 180.0))
(zero temp)
(set-m temp 1 1 1.0)
(set-m temp 0 0 (cos rd))
(set-m temp 0 2 (sin rd))
(set-m temp 2 0 (- 0 (sin rd)))
(set-m temp 2 2 (cos rd))
(set-m temp 3 3 1.0)
(mat-mult M temp M)
(destroy-matrix temp))
;;; Rotate around z axis
(define (rotz M r)
(define temp (new-matrix))
(define rd (/ (* r 3.14159) 180.0))
(zero temp)
(set-m temp 0 0 (cos rd))
(set-m temp 0 1 (- 0 (sin rd)))
(set-m temp 1 0 (sin rd))
(set-m temp 1 1 (cos rd))
(set-m temp 2 2 1.0)
(set-m temp 3 3 1.0)
(mat-mult M temp M)
(destroy-matrix temp))
;;; Scale a matrix
(define (scale M s)
(define temp (new-matrix))
(define (sloop m i s)
(if (< i 4) (begin
(set-m m i i s)
(sloop m (+ i 1) s))))
(zero temp)
(sloop temp 0 s)
(mat-mult M temp M)
(destroy-matrix temp))
;;; Make a matrix with random elements
(define (randmat M)
(define (rand-loop M i j)
(if (< i 4)
(if (< j 4)
(begin
(set-m M i j (drand48))
(rand-loop M i (+ j 1)))
(rand-loop M (+ i 1) 0))))
(rand-loop M 0 0))
;;; stray definitions colleced here
(define (rot-test M v t i)
(if (< i 360) (begin
(rotx M 1)
(rotz M -0.5)
(transform M v t)
(rot-test M v t (+ i 1)))))
(define (create-matrix i) ; Create some matrices
(if (< i 200)
(cons (new-matrix) (create-matrix (+ i 1)))
(list)))
(define (add-mat M ML)
(define (add-two m1 m2 i j)
(if (< i 4)
(if (< j 4)
(begin
(set-m m1 i j (+ (get-m m1 i j) (get-m m2 i j)))
(add-two m1 m2 i (+ j 1)))
(add-two m1 m2 (+ i 1) 0))))
(if (null? ML) () (begin
(add-two M (car ML) 0 0)
(add-mat M (cdr ML)))))
(define (cleanup ML)
(if (null? ML) () (begin
(destroy-matrix (car ML))
(cleanup (cdr ML)))))
(define (make-random ML) ; Put random values in them
(if (null? ML) () (begin
(randmat (car ML))
(make-random (cdr ML)))))
(define (mul-mat m ML)
(if (null? ML) () (begin
(mat-mult m (car ML) m)
(mul-mat m (cdr ML)))))
;;; Now we'll hammer on things a little bit just to make
;;; sure everything works.
(define M1 (new-matrix)) ; a matrix
(define v (createv 1 2 3 4)) ; a vector
(define t (createv 0 0 0 0)) ; the zero-vector
(define M-list (create-matrix 0)) ; get list of marices
(define M (new-matrix)) ; yet another matrix
(display "variables defined\n")
(define (do-test x)
(display "Testing matrix program...\n")
(identity M1)
(print-matrix M1)
(display "Rotate-x 45 degrees\n")
(rotx M1 45)
(print-matrix M1)
(display "Rotate y 30 degrees\n")
(roty M1 30)
(print-matrix M1)
(display "Rotate z 15 degrees\n")
(rotz M1 15)
(print-matrix M1)
(display "Scale 0.5\n")
(scale M1 0.5)
(print-matrix M1)
;; Rotating ...
(display "Rotating...\n")
(rot-test M1 v t 0)
(printv t)
(make-random M-list)
(zero M1)
(display "Adding them together (in Guile)\n")
(add-mat M1 M-list)
(print-matrix M1)
(display "Doing 200 multiplications (mostly in C)\n")
(randmat M)
(mul-mat M M-list)
(display "Cleaning up\n")
(cleanup M-list))
;;; matrix.scm ends here

View file

@ -0,0 +1,20 @@
// FILE : package.i
// See the SWIG users manual
%title "Matrix and vector package"
%module Matrix
%{
#include <math.h>
%}
%include guile/typemaps.i
%include guilemain.i
%include matrix.i
%include vector.i
// Include the math library so we can get some random numbers and
// other stuff
%include math.i
extern double drand48();

View file

@ -0,0 +1,42 @@
/* File : vector.c */
#include "vector.h"
Vector *createv(double x, double y, double z, double w) {
Vector *n;
n = (Vector *) malloc(sizeof(Vector));
n->x = x;
n->y = y;
n->z = z;
n->w = w;
return n;
}
/* Destroy vector */
void destroyv(Vector *v) {
free(v);
}
/* Print a vector */
void printv(Vector *v) {
printf("x = %g, y = %g, z = %g, w = %g\n", v->x, v->y, v->z, v->w);
}
/* Do a transformation */
void transform(double **m, Vector *v, Vector *r) {
r->x = m[0][0]*v->x + m[0][1]*v->y + m[0][2]*v->z + m[0][3]*v->w;
r->y = m[1][0]*v->x + m[1][1]*v->y + m[1][2]*v->z + m[1][3]*v->w;
r->z = m[2][0]*v->x + m[2][1]*v->y + m[2][2]*v->z + m[2][3]*v->w;
r->w = m[3][0]*v->x + m[3][1]*v->y + m[3][2]*v->z + m[3][3]*v->w;
}

View file

@ -0,0 +1,10 @@
#include <math.h>
typedef struct {
double x;
double y;
double z;
double w;
} Vector;

View file

@ -0,0 +1,24 @@
//
// FILE : vector.i
%init Vector_Init
%{
#include "vector.h"
%}
%section "Vector Operations"
extern Vector *createv(double x,double y,double z,double w);
/* Creates a new vector v(x,y,z,w) */
extern void destroyv(Vector *v);
/* Destroys the vector v */
extern void printv(Vector *v);
/* Prints out the vector v */
extern void transform(double **T, Vector *v, Vector *t);
/* Transforms vector c to vector t by M*v --> t */