;; -*-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 test257)


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


  (define-param-method 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-param-method display-matrix (%number)
		       (((mx (:diagonal-matrix %number)))
			<none>
			(nonpure))
    (let ((rows (field-ref mx 'len))
	  (columns (field-ref mx 'len)))
      (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-param-method display-matrix-nl (%number)
		       (((mx (:matrix %number)))
			<none>
			(nonpure))
    (display-matrix mx)
    (console-newline))


  (define-param-method display-matrix-nl (%number)
		       (((mx (:diagonal-matrix %number)))
			<none>
			(nonpure))
    (display-matrix mx)
    (console-newline))


  (define main
    (lambda (() <none> nonpure)
      (let ((mx1 (matrix (list
			     (list 1.0 0.0 2.0)
			     (list -1.5 2.5 0.0)
			     (list -3.0 -2.0 1.0))))
	    (mx2 (matrix (list
			     (list 0.0 0.0 -2.0)
			     (list 5.5 2.0 1.0)
			     (list 0.0 2.0 -1.0))))
	    (dm1 (diagonal-matrix (list 1.0 2.0 3.0)))
	    (dm2 (diagonal-matrix (list -2.3 -7.0 4.0)))
	    (cv1 (column-vector (list 5.1 10.5 -2.3)))
	    (cv2 (make-column-vector 3 2.0))
	    (rv1 (row-vector (list 5.1 10.5 -2.3)))
	    (rv2 (make-row-vector 3 -1.5)))

	(console-display-line "*")
	(console-newline)
	(display-matrix-nl (* mx1 mx2))
	(display-matrix-nl (* mx1 dm1))
	(display-matrix-nl (* dm1 mx1))
	(display-matrix-nl (* dm1 dm2))
	(display-matrix-nl (* mx1 cv1))
	(display-matrix-nl (* rv1 mx1))

	(console-display-line "+")
	(console-newline)
	(display-matrix-nl (+ mx1 mx2))
	(display-matrix-nl (+ mx1 dm1))
	(display-matrix-nl (+ dm1 mx1))
	(display-matrix-nl (+ cv1 cv2))
	(display-matrix-nl (+ rv1 rv2))

	(console-display-line "-")
	(console-newline)
	(display-matrix-nl (- mx1 mx2))
	(display-matrix-nl (- mx1 dm1))
	(display-matrix-nl (- dm1 mx1))
	(display-matrix-nl (- cv1 cv2))
	(display-matrix-nl (- rv1 rv2))

	(console-display-line "unary -")
	(console-newline)
	(display-matrix-nl (- mx1))
	(display-matrix-nl (- dm1))

	(console-display-line "/")
	(console-newline)
	(display-matrix-nl (/ mx1 2.5))
	(display-matrix-nl (/ dm1 2.5))))))
