File: test241.thp

package info (click to toggle)
theme-d 7.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,036 kB
  • sloc: lisp: 9,625; sh: 5,321; makefile: 715; ansic: 477
file content (117 lines) | stat: -rw-r--r-- 3,753 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;; -*-theme-d-*-

;; Copyright (C) 2014, 2024  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
    (parameters %number)
    (attributes equal-by-value)
    (constructor-access module)
    (fields
     (rows <integer> public hidden)
     (columns <integer> public hidden)
     (contents (:mutable-vector %number) public hidden)))
  
  
  (define-param-proc make-matrix (%number)
      (((rows <integer>) (columns <integer>)
                         (element-value %number))
       (:matrix %number)
       (force-pure))
    (create (:matrix %number) rows columns
      (make-mutable-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-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-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 (static-cast <real> 0.0)))
            (mx2 (make-matrix 4 2 (static-cast <real> 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))))))