diff examples/maths/QuadraticForm.java @ 1:5df24c91468d

Oh my what a mess.
author samer
date Fri, 05 Apr 2019 16:26:00 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/maths/QuadraticForm.java	Fri Apr 05 16:26:00 2019 +0100
@@ -0,0 +1,47 @@
+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);
+			}
+		};
+	}
+}