PageRenderTime 44ms CodeModel.GetById 25ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/PHPExcel/Shared/JAMA/examples/LMQuadTest.php

https://bitbucket.org/nfredricks/wp-employee-time
PHP | 116 lines | 63 code | 22 blank | 31 comment | 14 complexity | b6ff2ac85d9ae86774398c9f4b747fc5 MD5 | raw file
  1<?php
  2/**
  3 * quadratic (p-o)'S'S(p-o)
  4 * solve for o, S
  5 * S is a single scale factor
  6 */
  7class LMQuadTest {
  8
  9	/**
 10	 * @param array[] $x
 11	 * @param array[] $a
 12	 */
 13	function val($x, $a) {
 14		if (count($a) != 3) die ("Wrong number of elements in array a");
 15		if (count($x) != 2) die ("Wrong number of elements in array x");
 16
 17		$ox = $a[0];
 18		$oy = $a[1];
 19		$s  = $a[2];
 20
 21		$sdx = $s * ($x[0] - $ox);
 22		$sdy = $s * ($x[1] - $oy);
 23
 24		return ($sdx * $sdx) + ($sdy * $sdy);
 25   }	//	function val()
 26
 27
 28	/**
 29	 * z = (p-o)'S'S(p-o)
 30	 * dz/dp = 2S'S(p-o)
 31	 *
 32	 * z = (s*(px-ox))^2 + (s*(py-oy))^2
 33	 * dz/dox = -2(s*(px-ox))*s
 34	 * dz/ds = 2*s*[(px-ox)^2 + (py-oy)^2]
 35	 *
 36	 * z = (s*dx)^2 + (s*dy)^2
 37	 * dz/ds = 2(s*dx)*dx + 2(s*dy)*dy
 38	 *
 39	 * @param array[] $x
 40	 * @param array[] $a
 41	 * @param int $a_k
 42	 * @param array[] $a
 43	 */
 44	function grad($x, $a, $a_k) {
 45		if (count($a) != 3) die ("Wrong number of elements in array a");
 46		if (count($x) != 2) die ("Wrong number of elements in array x");
 47		if ($a_k < 3) die ("a_k=".$a_k);
 48
 49		$ox = $a[0];
 50		$oy = $a[1];
 51		$s  = $a[2];
 52
 53		$dx = ($x[0] - $ox);
 54		$dy = ($x[1] - $oy);
 55
 56		if ($a_k == 0)
 57			return -2.*$s*$s*$dx;
 58		elseif ($a_k == 1)
 59			return -2.*$s*$s*$dy;
 60		else
 61			return 2.*$s*($dx*$dx + $dy*$dy);
 62	}	//	function grad()
 63
 64
 65	/**
 66	 * @return array[] $a
 67	 */
 68	function initial() {
 69		$a[0] = 0.05;
 70		$a[1] = 0.1;
 71		$a[2] = 1.0;
 72
 73		return $a;
 74	}	//	function initial()
 75
 76
 77	/**
 78	 * @return Object[] $a
 79	 */
 80	function testdata() {
 81		$npts = 25;
 82
 83		$a[0] = 0.;
 84		$a[1] = 0.;
 85		$a[2] = 0.9;
 86
 87		$i = 0;
 88
 89		for ($r = -2; $r <= 2; ++$r) {
 90			for ($c = -2; $c <= 2; ++$c) {
 91				$x[$i][0] = $c;
 92				$x[$i][1] = $r;
 93				$y[$i] = $this->val($x[$i], $a);
 94				print("Quad ".$c.",".$r." -> ".$y[$i]."<br />");
 95				$s[$i] = 1.;
 96				++$i;
 97			}
 98		}
 99		print("quad x= ");
100
101		$qx = new Matrix($x);
102		$qx->print(10, 2);
103
104		print("quad y= ");
105		$qy = new Matrix($y, $npts);
106		$qy->print(10, 2);
107
108		$o[0] = $x;
109		$o[1] = $a;
110		$o[2] = $y;
111		$o[3] = $s;
112
113		return $o;
114	}	//	function testdata()
115
116}	//	class LMQuadTest