view examples/maths/QuadraticForm.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents 5df24c91468d
children
line wrap: on
line source
package eg.maths;
import  samer.core.*;
import  samer.maths.*;
import  samer.maths.ops.*;

public class QuadraticForm extends FunctionOfVector
{
	VVector				x, yy;
	Matrix				A;
	MatrixTimesVector	Ax;
	double []			y;

	public QuadraticForm() { this(Shell.getInt("dimensions",2)); }
	public QuadraticForm(int n) 
	{
		x = new VVector("x",n);
		yy = new VVector("y",n);
		A = new  Matrix("A",n,n);
		Ax = new MatrixTimesVector(yy.array(),A);
		y = yy.array();

		Shell.put("functionOfVector",this);
		Shell.put("vector",x);
	}

	public double apply(double [] x) 
	{ 
		Ax.run(x); 
		return -0.5*Mathx.dot(x,y);
	}

	public VectorFunctionOfVector derivative() 
	{	
		return new VectorFunctionOfVector() {

			public void apply(double [] x) {
				Ax.run(x);
				Mathx.copy(y,x);
			}

			public void apply(double [] x, double [] v) {
				Ax.run(x);
				Mathx.copy(y,v);
			}
		};
	}
}