/*
 * Encog(tm) Core v3.4 - Java Version
 * http://www.heatonresearch.com/encog/
 * https://github.com/encog/encog-java-core
 
 * Copyright 2008-2016 Heaton Research, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *   
 * For more information on Heaton Research copyrights, licenses 
 * and trademarks visit:
 * http://www.heatonresearch.com/copyright
 */
package org.encog.mathutil.matrices.hessian;

import org.encog.mathutil.EncogMath;
import org.encog.mathutil.error.ErrorCalculation;
import org.encog.mathutil.matrices.Matrix;
import org.encog.ml.data.MLData;
import org.encog.ml.data.MLDataPair;
import org.encog.ml.data.MLDataSet;
import org.encog.neural.networks.BasicNetwork;
import org.encog.util.EngineArray;

/**
 * Calculate the Hessian matrix using the finite difference method. This is a
 * very simple method of calculating the Hessian. The algorithm does not vary
 * greatly by number layers. This makes it very useful as a tool to check the
 * accuracy of other methods of determining the Hessian.
 * 
 * For more information on the Finite Difference Method see the following article.
 * 
 * http://en.wikipedia.org/wiki/Finite_difference_method
 * 
 */
public class HessianFD extends BasicHessian {
	
	/**
	 * The initial step size for dStep.
	 */
	public final double INITIAL_STEP = 0.001;
	
	
	/**
	 * The derivative step size, used for the finite difference method.
	 */
	private double[] dStep;
	
	/**
	 * The derivative coefficient, used for the finite difference method.
	 */
	private double[] dCoeff;
	
	/**
	 * The center of the point array.
	 */
	private int center;
	
	/**
	 * The number of points requested per side.  This determines the accuracy of the calculation.
	 */
	private int pointsPerSide = 5;
	
	/**
	 * The number of points actually used, which is (pointsPerSide*2)+1. 
	 */
	private int pointCount;
	
	/**
	 * The weight count.
	 */
	private int weightCount;
	
	/**
	 * {@inheritDoc}
	 */
	public void init(BasicNetwork theNetwork, MLDataSet theTraining) {
		
		super.init(theNetwork,theTraining);
		this.weightCount = theNetwork.getStructure().getFlat().getWeights().length;
		
		this.center = this.pointsPerSide+1;
		this.pointCount = (this.pointsPerSide*2)+1;
		this.dCoeff = createCoefficients();
		this.dStep = new double[this.weightCount];
		
		for (int i = 0; i < this.weightCount; i++) {
			this.dStep[i] = this.INITIAL_STEP;
		}

	}
	
	/**
	 * {@inheritDoc}
	 */
	public void compute() {
		this.sse = 0;
		
		for(int i=0;i 

Ads help maintain this website.