some matricial operations, very early development right now

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1639 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
vsc 2006-05-19 13:51:24 +00:00
parent 871bb9e024
commit ba3ea30e8e
3 changed files with 301 additions and 0 deletions

93
library/matrices.yap Normal file
View File

@ -0,0 +1,93 @@
/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-2006 *
* *
**************************************************************************
* *
* File: matrices.yap *
* Last rev: *
* mods: *
* comments: Have some fun with blobs *
* *
*************************************************************************/
/*
A matrix is an object with integer or floating point numbers. A matrix
may have a number of dimensions. These routines implement a number of
routine manipulation procedures.
matrix(Type,D1,D2,...,Dn,data(......))
Type = int, float
*/
:- module( matrices,
[
matrix_new/4,
matrix_dim/2,
matrix_type/2,
matrix_max/2]).
:- use_module(library_lists,
[append/3]).
matrix_new(Type,Dims,Data,Matrix) :-
PackedData =.. [data|Data],
append([Type,Dims],PackedData,MatInfo),
Matrix =.. [matrix|MatInfo].
matrix_dim(Matrix,Dim) :-
functor(Matrix,matrix,Arity),
Dim is Arity-2.
matrix_type(Matrix,Type) :-
arg(1, Matrix, Type).
Dim is Arity-2.
matrix_max(Matrix,Max) :-
arg(1, Matrix, Type).
typed_matrix_max(Type, Matrix, Max).
typed_matrix_max(int, Matrix, Max) :-
int_max_of_matrix(Matrix, Max, _).
typed_matrix_max(float, Matrix, Max) :-
float_max_of_matrix(Matrix, Max, _).
matrix_maxarg(Matrix,Max) :-
arg(1, Matrix, Type).
typed_matrix_maxarg(Type, Matrix, Max).
typed_matrix_maxarg(int, Matrix, Max) :-
int_max_of_matrix(Matrix, _, Max).
typed_matrix_maxarg(float, Matrix, Max) :-
float_max_of_matrix(Matrix, _, Max).
matrix_min(Matrix,Min) :-
arg(1, Matrix, Type).
typed_matrix_min(Type, Matrix, Min).
typed_matrix_min(int, Matrix, Min) :-
int_min_of_matrix(Matrix, Min, _).
typed_matrix_min(float, Matrix, Min) :-
float_min_of_matrix(Matrix, Min, _).
matrix_minarg(Matrix,Min) :-
arg(1, Matrix, Type).
typed_matrix_minarg(Type, Matrix, Min).
typed_matrix_minarg(int, Matrix, Min) :-
int_min_of_matrix(Matrix, _, Min).
typed_matrix_minarg(float, Matrix, Min) :-
float_min_of_matrix(Matrix, _, Min).

View File

@ -0,0 +1,58 @@
#
# default base directory for YAP installation
# (EROOT for architecture-dependent files)
#
prefix = @prefix@
ROOTDIR = $(prefix)
EROOTDIR = @exec_prefix@
#
# where the binary should be
#
BINDIR = $(EROOTDIR)/bin
#
# where YAP should look for libraries
#
LIBDIR=$(EROOTDIR)/lib/Yap
#
#
CC=@CC@
CFLAGS= @CFLAGS@ $(YAP_EXTRAS) $(DEFS) -I$(srcdir) -I../.. -I$(srcdir)/../../include
#
#
# You shouldn't need to change what follows.
#
INSTALL=@INSTALL@
INSTALL_DATA=@INSTALL_DATA@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
SHELL=/bin/sh
RANLIB=@RANLIB@
srcdir=@srcdir@
SHLIB_CFLAGS=@SHLIB_CFLAGS@
SHLIB_SUFFIX=@SHLIB_SUFFIX@
#4.1VPATH=@srcdir@:@srcdir@/OPTYap
CWD=$(PWD)
#
OBJS=matrices.o
SOBJS=matrices@SHLIB_SUFFIX@
#in some systems we just create a single object, in others we need to
# create a libray
all: $(SOBJS)
matrices.o: $(srcdir)/matrices.c
$(CC) -c $(CFLAGS) $(SHLIB_CFLAGS) $(srcdir)/matrices.c -o matrices.o
@DO_SECOND_LD@%@SHLIB_SUFFIX@: %.o
@DO_SECOND_LD@ @SHLIB_LD@ -o $@ $<
@DO_SECOND_LD@matrices@SHLIB_SUFFIX@: matrices.o
@DO_SECOND_LD@ @SHLIB_LD@ -o matrices@SHLIB_SUFFIX@ matrices.o
install: all
$(INSTALL_PROGRAM) $(SOBJS) $(DESTDIR)$(LIBDIR)
clean:
rm -f *.o *~ $(OBJS) $(SOBJS) *.BAK

150
library/matrices/matrices.c Normal file
View File

@ -0,0 +1,150 @@
/*************************************************************************
* *
* YAP Prolog *
* *
* Yap Prolog was developed at NCCUP - Universidade do Porto *
* *
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: random.c *
* Last rev: *
* mods: *
* comments: regular expression interpreter *
* *
*************************************************************************/
#include "config.h"
#include "YapInterface.h"
#include <math.h>
#if defined(__MINGW32__) || _MSC_VER
#include <windows.h>
#endif
void PROTO(init_matrices, (void));
static int
int_min_of_matrix(void)
{
Term t = YAP_ARG1;
YAP_UInt dim = YAP_ArityOfFunctor(YAP_FunctorOfTerm(t)), i, pos;
YAP_Int *mat = (YAP_Int *)malloc( dim*sizeof(YAP_Int) ), min;
if (!mat)
return FALSE;
if (!YAP_ArgsToIntArray(t, dim, mat))
return FALSE;
min = mat[0];
pos = 0;
for (i = 1; i < dim; i++) {
if (mat[i] < min) {
min = mat[i];
pos = i;
}
}
return YAP_unify(YAP_ARG2, YAP_MkIntTerm(min)) &&
YAP_unify(YAP_ARG3, YAP_MkIntTerm(pos));
}
static int
float_min_of_matrix(void)
{
Term t = YAP_ARG1;
YAP_UInt dim = YAP_ArityOfFunctor(YAP_FunctorOfTerm(t)), i, pos;
YAP_Float *mat = (YAP_Float *)malloc( dim*sizeof(YAP_Float) ), min;
if (!mat)
return FALSE;
if (!YAP_ArgsToFloatArray(t, dim, mat))
return FALSE;
min = mat[0];
pos = 0;
for (i = 1; i < dim; i++) {
if (mat[i] < min) {
min = mat[i];
pos = i;
}
}
return YAP_unify(YAP_ARG2, YAP_MkFloatTerm(min)) &&
YAP_unify(YAP_ARG3, YAP_MkIntTerm(pos));
}
static int
int_max_of_matrix(void)
{
Term t = YAP_ARG1;
YAP_UInt dim = YAP_ArityOfFunctor(YAP_FunctorOfTerm(t)), i, pos;
YAP_Int *mat = (YAP_Int *)malloc( dim*sizeof(YAP_Int) ), max;
if (!mat)
return FALSE;
if (!YAP_ArgsToIntArray(t, dim, mat))
return FALSE;
max = mat[0];
pos = 0;
for (i = 1; i < dim; i++) {
if (mat[i] > max) {
max = mat[i];
pos = i;
}
}
return YAP_unify(YAP_ARG2, YAP_MkIntTerm(min)) &&
YAP_unify(YAP_ARG3, YAP_MkIntTerm(pos));
}
static int
float_max_of_matrix(void)
{
Term t = YAP_ARG1;
YAP_UInt dim = YAP_ArityOfFunctor(YAP_FunctorOfTerm(t)), i, pos;
YAP_Float *mat = (YAP_Float *)malloc( dim*sizeof(YAP_Float) ), max;
if (!mat)
return FALSE;
if (!YAP_ArgsToFloatArray(t, dim, mat))
return FALSE;
max = mat[0];
pos = 0;
for (i = 1; i < dim; i++) {
if (mat[i] > max) {
max = mat[i];
pos = i;
}
}
return YAP_unify(YAP_ARG2, YAP_MkFloatTerm(min)) &&
YAP_unify(YAP_ARG3, YAP_MkIntTerm(pos));
}
void
init_matrices(void)
{
YAP_UserCPredicate("int_max_of_matrix", int_max_of_matrix, 3);
YAP_UserCPredicate("float_max_of_matrix", float_max_of_matrix, 3);
YAP_UserCPredicate("int_min_of_matrix", int_min_of_matrix, 3);
YAP_UserCPredicate("float_min_of_matrix", float_min_of_matrix, 3);
}
#ifdef _WIN32
int WINAPI PROTO(win_matrices, (HANDLE, DWORD, LPVOID));
int WINAPI win_matrices(HANDLE hinst, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return 1;
}
#endif