wiki:MathLibrary
Last modified 12 years ago Last modified on 09/27/06 15:01:16

return to XbrainEvaluation

module namespace math = "http://sig.biostr.washington.edu/dxq/math/";  
declare namespace javamath   = "java:java.lang.Math";

declare function math:sqrt($number as xs:double) as xs:double
{
	javamath:sqrt($number)
};

declare function math:pow($number as xs:double, $exp as xs:integer) as xs:double
{
	javamath:pow($number,$exp)
};

declare function math:distance3D($p0 as element(point), $p1 as element(point)) as xs:double
{
	let $x0 := $p0/x
	let $y0 := $p0/y
	let $z0 := $p0/z
	
	let $x1 := $p1/x
	let $y1 := $p1/y
	let $z1 := $p1/z
	
	let $x_diff_sq := math:pow($x0 - $x1,2)
	let $y_diff_sq := math:pow($y0 - $y1,2)
	let $z_diff_sq := math:pow($z0 - $z1,2)
	
	let $distance := math:sqrt($x_diff_sq + $y_diff_sq + $z_diff_sq)
	
	return
		$distance
};

declare function math:standard_deviation($points as element(point)+) as xs:double
{
	let $count := count($points)
	return
		if($count=1)
		then
			0
		else
			let $meanPoint := math:mean_point($points)
			let $dev_sq :=
				for $point in $points
				return
					math:pow(math:distance3D($point,$meanPoint),2)
			let $stnd_dev := math:sqrt(sum($dev_sq) div count($points) - 1)
			return
				$stnd_dev
};

declare function math:mean_point($points as element(point)*) as element(point)
{
	<point>
		<x>{avg($points/x)}</x>
		<y>{avg($points/y)}</y>
		<z>{avg($points/x)}</z>
	</point>
};