On 01/02/2014 23:33, Hermann Stamm-Wilbrandt wrote:

Last Sylvester there was a thread on Matrix Multiplication in
XQuery:
http://markmail.org/message/7q7qnbbnjo7cljzv?q=list:com.x-query.talk+matrix+multiplication




The biggest problem identified was that XQuery does not allow for
efficient representation of 2+dimensional arrays.


well it does, but as in other languages that only really have 1-D arrays
(or languages such as C or Fortran where 2D arrays are a thin veneer
over 1D arrays), you need to store a 2 D array as a 1D array with an
additional integer giving the stride or leading dimension (in row or
column order). To keep the arrays self contained I stored this as the first item in each sequence in the example below.




JSON does provide 2+dimensional arrays for free.

I did not measure performance yet, but this JSONiq script looks very
similar to what would be done in C:
http://try.zorba.io/queries/xquery/jFd3Q8f82HuZGzcYDzQpdN4SdfY=

declare variable $A := [ [1,2], [3,4], [5,6], [7,8] ]; declare
variable $B := [ [1,2,3], [4,5,6] ];

[ for $i in 1 to count(jn:members($A)) return [ for $k in 1 to
count(jn:members($B(1))) return fn:sum( for $j in 1 to
count(jn:members($B)) return $A($i)($j) * $B($j)($k) ) ] ]



In Xquery 1 you could do


let $a:=(2, (:2 columns :)
1,2,
3,4,
5,6,
7,8),

$b:=(3, (:3 columns :)
1,2,3,
4,5,6)

return

(
$b[1],
for $i in 1 to xs:int((count($a) -1) div $a[1]),
$j in 1 to xs:int($b[1])
return
sum(
for $k in 1 to xs:int($a[1]) return
($a[($i -1)*$a[1]+$k+1] * $b[($k -1)*$b[1]+$j+1])
)
)


which produces

3 (:3 columns :)
  9 12 15
 19 26 33
 29 40 51
 39 54 69



And much simpler than in XSLT:
http://rosettacode.org/wiki/Matrix_multiplication#XSLT_1.0:



As the above is in fact only Xpath 2, you could do the identical
expression in XSLT 2


David

_______________________________________________
[email protected]
http://x-query.com/mailman/listinfo/talk

Reply via email to