Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

gnn_constant_input.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *  @file gnn_memory_input.c
00003  *  @brief Input Set Residing in Memory Implementation.
00004  *
00005  *  @date   : 22-09-03 01:21
00006  *  @author : Pedro Ortega C. <peortega@dcc.uchile.cl>
00007  *  Copyright  2003  Pedro Ortega C.
00008  ****************************************************************************/
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024 
00025 
00026 
00027 /**
00028  * @defgroup gnn_constant_input_doc gnn_constant_input : Constant Input Samples.
00029  * @ingroup gnn_input_doc
00030  * @brief Constant Input samples.
00031  *
00032  * The \ref gnn_constant_input provides a single constant sample.
00033  */
00034 
00035 
00036 
00037 /******************************************/
00038 /* Include Files                          */
00039 /******************************************/
00040 
00041 #include "gnn_utilities.h"
00042 #include "gnn_constant_input.h"
00043 
00044 
00045 
00046 /******************************************/
00047 /* Static Declaration                     */
00048 /******************************************/
00049 
00050 /**
00051  * @brief The datatype for the constant samples set.
00052  * @ingroup gnn_memory_input_doc
00053  *
00054  * This is the datatype for constant input sample set. It extends the basic
00055  * \ref gnn_input structure to include the additional pointer
00056  * to the sample vector.
00057  */
00058 typedef struct _gnn_constant_input gnn_constant_input;
00059 
00060 struct _gnn_constant_input
00061 {
00062     gnn_input   set;    /**< The underlying \ref gnn_input */
00063     gsl_vector *vector; /**< The sample.                   */
00064 };
00065 
00066 static const gsl_vector *
00067 gnn_constant_input_get (gnn_input *set, size_t k);
00068 
00069 static void
00070 gnn_constant_input_destroy (gnn_input *set);
00071 
00072 
00073 
00074 /******************************************/
00075 /* Static Implementation                  */
00076 /******************************************/
00077 
00078 /**
00079  * @brief The "get" function.
00080  * @ingroup gnn_constant_input_doc
00081  *
00082  * This function is returns the k-th pattern in the set.
00083  *
00084  * @param  set A pointer to a \ref gnn_constant.
00085  * @param  k   The index of the pattern to be retrieved.
00086  * @return A pointer to the sample vector.
00087  */
00088 static const gsl_vector *
00089 gnn_constant_input_get (gnn_input *set, size_t k)
00090 {
00091     gnn_constant_input *cset;
00092 
00093     assert (set != NULL);
00094 
00095     /* get view as a constant sampler */
00096     cset = (gnn_constant_input *) set;
00097 
00098     return cset->vector;
00099 }
00100 
00101 /**
00102  * @brief Destroy function.
00103  * @ingroup gnn_constant_input_doc
00104  *
00105  * This is the \ref gnn_constant_input destroy function.
00106  * @param set A pointer to a \ref gnn_constant_input sampler.
00107  */
00108 static void
00109 gnn_constant_input_destroy (gnn_input *set)
00110 {
00111     gnn_constant_input *cset;
00112 
00113     assert (set != NULL);
00114     
00115     cset = (gnn_constant_input *) set;
00116     
00117     if (cset->vector != NULL)
00118         gsl_vector_free (cset->vector);
00119 }
00120 
00121 
00122 
00123 /******************************************/
00124 /* Public Interface                       */
00125 /******************************************/
00126 
00127 /**
00128  * @brief Builds an input set from a vector.
00129  * @ingroup gnn_constant_input_doc
00130  *
00131  * This function creates a new \ref gnn_constant_input from a vector.
00132  *
00133  * @param  v The sample vector.
00134  * @return Returns a pointer to a new \ref gnn_constant_input set.
00135  */
00136 gnn_input *
00137 gnn_constant_input_new_from_vector (const gsl_vector *v)
00138 {
00139     int status;
00140     gnn_input *set;
00141     gnn_constant_input *cset;
00142     
00143     assert (v != NULL);
00144     
00145     /* create the set */
00146     set = gnn_constant_input_new (v->size, 0.0);
00147     if (set == NULL)
00148     {
00149         GSL_ERROR_VAL ("could not create gnn_constant_input",
00150                        GSL_EFAILED, NULL);
00151     }
00152     
00153     /* get view as a gnn_constant_input */
00154     cset = (gnn_constant_input *) set;
00155     
00156     /* copy vector values */
00157     gsl_vector_memcpy (cset->vector, v);
00158     
00159     return set;
00160 }
00161 
00162 /**
00163  * @brief Builds a input set with the given value.
00164  * @ingroup gnn_constant_input_doc
00165  *
00166  * This function creates a new \ref gnn_constant_input. The resulting
00167  * input sampler has a single sample vector of the given size. All its elements
00168  * have the same value.
00169  *
00170  * @param  n     The size of the sample.
00171  * @param  value The values for the sample's elements.
00172  * @return Returns a pointer to a new \ref gnn_constant_input set.
00173  */
00174 gnn_input *
00175 gnn_constant_input_new (size_t n, double value)
00176 {
00177     int status;
00178     gnn_input *set;
00179     gnn_constant_input *cset;
00180 
00181     /* alloc memory for input set */
00182     cset = (gnn_constant_input *) malloc (sizeof (gnn_constant_input));
00183 
00184     /* get view as a input set */
00185     set = (gnn_input *) cset;
00186 
00187     /* initialize */
00188     status = gnn_input_init (set,
00189                              1,
00190                              n,
00191                              NULL,
00192                              gnn_constant_input_get,
00193                              gnn_constant_input_destroy);
00194     if (status)
00195     {
00196         gnn_input_destroy (set);
00197         GSL_ERROR_VAL ("could not initialize gnn_constant_input",
00198                        GSL_EINVAL, NULL);
00199     }
00200 
00201     /* create sample */
00202     cset->vector = gsl_vector_alloc (n);
00203     gsl_vector_set_all (cset->vector, value);
00204 
00205     return set;
00206 }
00207 
00208 

Generated on Sun Jun 13 20:50:11 2004 for libgnn Gradient Retropropagation Machine Library by doxygen1.2.18