AI-LibNeural

 view release on metacpan or  search on metacpan

LibNeural.xs  view on Meta::CPAN

/*
 * $Header$
 *
 * this is based off of code that i based off of other modules i've found in the
 * distant past. if you are the original author and you recognize this code let
 * me know and you'll be credited
 *
 * Copyright (C) 2003 by Ross McFarland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This library 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.  See the GNU
 * Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA  02111-1307  USA.
 *
 */

#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
	
#include <nnwork.h>
#include <neuron.h>

static int
not_here(char *s)
{
    croak("%s not implemented on this architecture", s);
    return -1;
}

static double
constant(char *name, int len, int arg)
{
    errno = 0;
    switch (name[0 + 0]) {
    case 'A':
	if (strEQ(name + 0, "ALL")) {	/*  removed */
#ifdef ALL
	    return ALL;
#else
	    goto not_there;
#endif
	}
    case 'H':
	if (strEQ(name + 0, "HIDDEN")) {	/*  removed */
#ifdef HIDDEN
	    return HIDDEN;
#else
	    goto not_there;
#endif
	}
    case 'I':
	if (strEQ(name + 0, "INPUT")) {	/*  removed */
#ifdef INPUT
	    return INPUT;
#else
	    goto not_there;
#endif
	}
    case 'O':
	if (strEQ(name + 0, "OUTPUT")) {	/*  removed */
#ifdef OUTPUT
	    return OUTPUT;
#else
	    goto not_there;
#endif
	}
    }
    errno = EINVAL;
    return 0;

not_there:
    errno = ENOENT;
    return 0;
}

/* function that takes an array reference and convert it into an equivelent
 * float array. dlen is the number of elements that we want to make sure are in
 * the array */
static float *
svpvav_to_float_array (SV * svpvav, int dlen)
{
	float *  array;
	AV    *  avp;
	SV    ** svpp;
	int      i;

	/* make sure that svpvav is array reference */
	if( !SvROK(svpvav) || (SvTYPE(SvRV(svpvav)) != SVt_PVAV) )
		Perl_croak(aTHX_ "parameter should be a valid array reference");

	/* get the array pointers out of its sv reference */
	avp = (AV*)SvRV(svpvav);

	/* make sure that it has the desired number of elements */
	if( av_len(avp)+1 != dlen )
		Perl_croak(aTHX_ "size of array and desired length do not match");
	
	/* alloc the memory for ains and aouts */
	array = (float*)malloc( dlen * sizeof(float) );
	if( array == NULL )
		Perl_croak(aTHX_ "unable to allocate memory for storing array");

	/* copy avins to ains */
	for( i = 0; i < dlen; i++ )
	{
		/* don't need ins anymore use as a tmp */
		svpp = av_fetch(avp, i, 0);
		if( !svpp || !*svpp || !SvOK(*svpp) )
		{
			if( array ) free(array);
			Perl_croak(aTHX_ "bad array value encountered at index %d", i);
		}
		array[i] = (float)SvNV(*svpp);
	}

	return array;
}

MODULE = AI::LibNeural		PACKAGE = AI::LibNeural		

double
constant(sv,arg)
    PREINIT:
	STRLEN		len;
    INPUT:
	SV *		sv
	char *		s = SvPV(sv, len);
	int		arg
    CODE:
	RETVAL = constant(s,len,arg);
    OUTPUT:



( run in 0.692 second using v1.01-cache-2.11-cpan-9581c071862 )