| 1 | return to XbrainEvaluation |

| 2 | {{{ |

| 3 | module namespace math = "http://sig.biostr.washington.edu/dxq/math/"; |

| 4 | declare namespace javamath = "java:java.lang.Math"; |

| 5 | |

| 6 | declare function math:sqrt($number as xs:double) as xs:double |

| 7 | { |

| 8 | javamath:sqrt($number) |

| 9 | }; |

| 10 | |

| 11 | declare function math:pow($number as xs:double, $exp as xs:integer) as xs:double |

| 12 | { |

| 13 | javamath:pow($number,$exp) |

| 14 | }; |

| 15 | |

| 16 | declare function math:distance3D($p0 as element(point), $p1 as element(point)) as xs:double |

| 17 | { |

| 18 | let $x0 := $p0/x |

| 19 | let $y0 := $p0/y |

| 20 | let $z0 := $p0/z |

| 21 | |

| 22 | let $x1 := $p1/x |

| 23 | let $y1 := $p1/y |

| 24 | let $z1 := $p1/z |

| 25 | |

| 26 | let $x_diff_sq := math:pow($x0 - $x1,2) |

| 27 | let $y_diff_sq := math:pow($y0 - $y1,2) |

| 28 | let $z_diff_sq := math:pow($z0 - $z1,2) |

| 29 | |

| 30 | let $distance := math:sqrt($x_diff_sq + $y_diff_sq + $z_diff_sq) |

| 31 | |

| 32 | return |

| 33 | $distance |

| 34 | }; |

| 35 | |

| 36 | declare function math:standard_deviation($points as element(point)+) as xs:double |

| 37 | { |

| 38 | let $count := count($points) |

| 39 | return |

| 40 | if($count=1) |

| 41 | then |

| 42 | 0 |

| 43 | else |

| 44 | let $meanPoint := math:mean_point($points) |

| 45 | let $dev_sq := |

| 46 | for $point in $points |

| 47 | return |

| 48 | math:pow(math:distance3D($point,$meanPoint),2) |

| 49 | let $stnd_dev := math:sqrt(sum($dev_sq) div count($points) - 1) |

| 50 | return |

| 51 | $stnd_dev |

| 52 | }; |

| 53 | |

| 54 | declare function math:mean_point($points as element(point)*) as element(point) |

| 55 | { |

| 56 | <point> |

| 57 | <x>{avg($points/x)}</x> |

| 58 | <y>{avg($points/y)}</y> |

| 59 | <z>{avg($points/x)}</z> |

| 60 | </point> |

| 61 | }; |

| 62 | }}} |