;; -*-theme-d-*-

;; Copyright (C) 2014  Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.

;; Expected results: translation and running OK


(define-proper-program (tests test241)


  (import (standard-library core)
	  (standard-library console-io))


  (define-param-class :matrix (%number) <object> #t #f #t module ()
    ((rows <integer> public hidden)
     (columns <integer> public hidden)
     (contents (:mutable-value-vector %number) public hidden)))


  (define-param-proc make-matrix (%number)
		     (((rows <integer>) (columns <integer>)
		       (element-value %number))
		      (:matrix %number)
		      (force-pure))
    (make (:matrix %number) rows columns
	  (make-mutable-value-vector %number (* rows columns) element-value)))


  (define-param-proc matrix-ref (%number)
		     (((mx (:matrix %number))
		       (i-row <integer>)
		       (i-column <integer>))
		      %number
		      (force-pure))
    (let ((rows (field-ref mx 'rows))
	  (columns (field-ref mx 'columns)))
      (assert (and (>= i-row 0) (< i-row rows)))
      (assert (and (>= i-column 0) (< i-column columns)))
      (mutable-value-vector-ref
       (field-ref mx 'contents)
       (+ (* i-row columns) i-column))))


  (define-param-proc matrix-set! (%number)
		     (((mx (:matrix %number))
		       (i-row <integer>)
		       (i-column <integer>)
		       (element-value %number))
		      <none>
		      (nonpure))
    (let ((rows (field-ref mx 'rows))
	  (columns (field-ref mx 'columns)))
      (assert (and (>= i-row 0) (< i-row rows)))
      (assert (and (>= i-column 0) (< i-column columns)))
      (mutable-value-vector-set!
       (field-ref mx 'contents)
       (+ (* i-row columns) i-column) element-value)))


  (define-param-proc matrix* (%number)
		     (((mx1 (:matrix %number)) (mx2 (:matrix %number)))
		      (:matrix %number)
		      (force-pure))
    (let ((rows1 (field-ref mx1 'rows))
	  (columns1 (field-ref mx1 'columns))
	  (rows2 (field-ref mx2 'rows))
	  (columns2 (field-ref mx2 'columns)))
      (assert (= columns1 rows2))
      (let ((result (make-matrix rows1 columns2 (zero %number))))
	(do ((i1 <integer> 0 (+ i1 1))) ((>= i1 rows1))
	  (do ((i2 <integer> 0 (+ i2 1))) ((>= i2 columns2))
	    (let-mutable ((sum %number (zero %number)))
	      (do ((i3 <integer> 0 (+ i3 1))) ((>= i3 columns1))
		(set! sum
		      (+ sum
			 (* (matrix-ref mx1 i1 i3)
			    (matrix-ref mx2 i3 i2)))))
	      (matrix-set! result i1 i2 sum))))
	result)))


  (define-param-proc display-matrix (%number)
		     (((mx (:matrix %number)))
		       <none>
		      (nonpure))
    (let ((rows (field-ref mx 'rows))
	  (columns (field-ref mx 'columns)))
      (do ((i1 <integer> 0 (+ i1 1))) ((>= i1 rows))
	(begin
	  (do ((i2 <integer> 0 (+ i2 1))) ((>= i2 columns))
	    (console-display (matrix-ref mx i1 i2))
	    (if (< i2 (- columns 1))
		(console-display-string " ")))
	  (console-newline)))))


  (define main
    (lambda (() <none> nonpure)
      (let ((mx1 (make-matrix 3 4 0.0))
	    (mx2 (make-matrix 4 2 0.0)))
	(matrix-set! mx1 0 0 1.0)
	(matrix-set! mx1 1 1 1.5)
	(matrix-set! mx1 2 1 -3.0)
	(matrix-set! mx1 2 3 10.0)
	(matrix-set! mx2 0 1 -3.5)
	(matrix-set! mx2 1 1 2.3)
	(matrix-set! mx2 2 0 1.2)
	(matrix-set! mx2 2 1 1.2)
	(let ((result (matrix* mx1 mx2)))
	  (display-matrix result))))))
