/* FFNet_Matrix.c */
/* David Weenink, 6/2/95 */

#include "FFNet_Matrix.h"

Matrix FFNet_weightsToMatrix (FFNet me, long layer, int deltaWeights)
{
	Matrix thee = NULL; long i, node = 1;
	if (layer < 1 || layer > my nLayers) return Melder_errorp ("FFNet_weightsToMatrix: "
		"layer must be > 0 and < %ld.", my nLayers);
	if (! (thee = Matrix_create (
		0.5, my nUnitsInLayer[layer]+0.5, my nUnitsInLayer[layer], 1, 1,
		0.5, my nUnitsInLayer[layer-1]+1+0.5, my nUnitsInLayer[layer-1]+1, 1, 1)
		 )) return thee;
	for (i=0; i < layer; i++) node += my nUnitsInLayer[i] + 1;
	for (i=1; i <= my nUnitsInLayer[layer]; i++, node++)
	{
		long j, k = 1;
		for (j=my wFirst[node]; j <= my wLast[node]; j++)
			thy z[k++][i] = deltaWeights ? my dwi[j] : my w[j];
	}
	return thee;	
}

FFNet FFNet_weightsFromMatrix (FFNet me, Matrix him, long layer)
{
	FFNet thee = NULL; long i, node = 1;
	if (layer < 1 || layer > my nLayers) return Melder_errorp ("FFNet_weightsFromMatrix: "
		"layer must be > 0 and < %ld.", my nLayers);
	if (my nUnitsInLayer[layer] != his nx) return Melder_errorp ("FFNet_weightsFromMatrix:"
		"#columns in Matrix must equal #units in layer %ld (%ld)", layer, my nUnitsInLayer[layer]);
	if (my nUnitsInLayer[layer-1]+1 != his ny) return Melder_errorp ("FFNet_weightsFromMatrix:"
		"#rows in Matrix must equal (#units in layer %ld) + 1 (%ld).",
		layer-1, my nUnitsInLayer[layer-1] + 1);
	if (! (thee = Data_copy (me))) return NULL;
	for (i=0; i < layer; i++) node += thy nUnitsInLayer[i] + 1;
	for (i=1; i <= thy nUnitsInLayer[layer]; i++, node++)
	{
		long j, k = 1;
		for (j=thy wFirst[node]; j <= thy wLast[node]; j++, k++)
			 thy w[j] = his z[k][i];
	}
	return thee;
}


/* End of file FFNet_Matrix.c */
