source: project/release/4/flsim/trunk/sml-lib/tensor/tensortest.sml @ 29960

Last change on this file since 29960 was 29960, checked in by Ivan Raikov, 8 years ago

flsim: added tensor library to sml support files

File size: 11.9 KB
Line 
1
2(*
3 Copyright (c) Ivan Raikov.
4 All rights reserved.
5
6Redistribution and use in source and binary forms, with or
7without modification, are permitted provided that the following
8conditions are met:
9
101. Redistributions of source code must retain the above copyright
11   notice, this list of conditions and the following disclaimer.
12
132. Redistributions in binary form must reproduce the above
14   copyright notice, this list of conditions and the following
15   disclaimer in the documentation and/or other materials provided
16   with the distribution.
17
183. All advertising materials mentioning features or use of this
19   software must display the following acknowledgement:
20        This product includes software developed by Ivan Raikov.
21
22*)
23
24
25structure TensorTest =
26struct
27
28fun putStrLn (file, str) = 
29    (TextIO.output (file, str);
30     TextIO.output (file, "\n"))
31   
32fun putStr (file, str) = 
33    (TextIO.output (file, str))
34
35
36
37
38val _ = print "fromList: "
39
40val A = RTensor.fromList ([4,2],List.tabulate (8, fn i => Real.fromInt i))
41val _ = TensorFile.realTensorWrite (TextIO.stdOut) A
42val v = RTensor.sub (A,[0,0])
43val _ = (print "A(0,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
44val v = RTensor.sub (A,[0,1])
45val _ = (print "A(0,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
46val v = RTensor.sub (A,[1,0])
47val _ = (print "A(1,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
48val v = RTensor.sub (A,[1,1])
49val _ = (print "A(1,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
50val v = RTensor.sub (A,[2,0])
51val _ = (print "A(2,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
52val v = RTensor.sub (A,[2,1])
53val _ = (print "A(2,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
54val v = RTensor.sub (A,[3,0])
55val _ = (print "A(3,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
56val v = RTensor.sub (A,[3,1])
57val _ = (print "A(3,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
58
59val _ = print "slice: "
60val sl = RTensorSlice.slice ([([3,0],[3,1]),([1,0],[1,1])],A)
61val _  = TensorFile.realTensorWrite (TextIO.stdOut) (RTensorSlice.map (fn (x) => x) sl)
62
63val _ = print "realRandomTensor: "
64
65fun realRandomTensor (xseed,yseed) shape =
66    let 
67        val length = Index.length shape
68        val seed   = Random.rand (xseed,yseed)
69        val a      = RTensor.Array.array(length, Random.randReal seed)
70        fun loop 0 = RTensor.fromArray(shape, a)
71          | loop j = (RTensor.Array.update(a, length-j, Random.randReal seed);
72                      loop (j-1))
73    in 
74        loop (length - 1)
75    end
76
77val N1 = 8
78val N2 = 2
79val N3 = 10
80val N =  N1+N2
81
82val SN1 = (RTensor.*> 0.5 (realRandomTensor (13,17) [N,N1]) )
83val SN2 = (RTensor.~ (realRandomTensor (19,23) [N,N2]))
84
85val _ = TensorFile.realTensorWrite (TextIO.stdOut) SN1
86val _ = TensorFile.realTensorWrite (TextIO.stdOut) SN2
87
88val SN  = RTensor.cat (SN1, SN2, 1)
89val _ = TensorFile.realTensorWrite (TextIO.stdOut) SN
90
91val SN3 = RTensor.new ([N,N3],10.0)
92val _ = TensorFile.realTensorWrite (TextIO.stdOut) SN3
93
94val SN'  = RTensor.cat (SN, SN3, 1)
95val _ = TensorFile.realTensorWrite (TextIO.stdOut) SN'
96
97val S0  = RTensor.fromList ([2,2],[1.0,2.0,3.0,4.0])
98val _ = (print "S0 = "; TensorFile.realTensorWrite (TextIO.stdOut) S0)
99val v = RTensor.sub (S0,[0,0])
100val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
101val v = RTensor.sub (S0,[0,1])
102val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
103val v = RTensor.sub (S0,[1,0])
104val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
105val v = RTensor.sub (S0,[1,1])
106val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
107
108val v = RTensor.sub (SN1,[0,0])
109val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
110val v = RTensor.sub (SN1,[0,1])
111val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
112val v = RTensor.sub (SN1,[1,0])
113val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
114val v = RTensor.sub (SN1,[1,1])
115val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
116val v = RTensor.sub (SN1,[2,0])
117val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
118val v = RTensor.sub (SN1,[2,1])
119val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
120
121val _ = putStrLn (TextIO.stdOut, "prepad:")
122val ones = RTensor.new ([2,2],1.0)
123val S  = RTensor.prepad (ones, 2, 0.0, 1)
124val _ = TensorFile.realTensorWrite (TextIO.stdOut) S
125val v = RTensor.sub (S,[0,0])
126val _ = (print "S(0,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
127val v = RTensor.sub (S,[0,1])
128val _ = (print "S(0,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
129val v = RTensor.sub (S,[0,2])
130val _ = (print "S(0,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
131val v = RTensor.sub (S,[0,3])
132val _ = (print "S(0,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
133val v = RTensor.sub (S,[1,0])
134val _ = (print "S(1,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
135val v = RTensor.sub (S,[1,1])
136val _ = (print "S(1,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
137val v = RTensor.sub (S,[1,2])
138val _ = (print "S(1,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
139val v = RTensor.sub (S,[1,3])
140val _ = (print "S(1,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
141
142val _ = putStrLn (TextIO.stdOut, "prepad:")
143val ones = RTensor.new ([2,2],1.0)
144val S  = RTensor.prepad (ones, 2, 0.0, 0)
145val _ = TensorFile.realTensorWrite (TextIO.stdOut) S
146val v = RTensor.sub (S,[0,0])
147val _ = (print "S(0,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
148val v = RTensor.sub (S,[1,0])
149val _ = (print "S(1,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
150val v = RTensor.sub (S,[2,0])
151val _ = (print "S(2,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
152val v = RTensor.sub (S,[3,0])
153val _ = (print "S(3,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
154val v = RTensor.sub (S,[0,1])
155val _ = (print "S(0,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
156val v = RTensor.sub (S,[1,1])
157val _ = (print "S(1,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
158val v = RTensor.sub (S,[2,1])
159val _ = (print "S(2,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
160val v = RTensor.sub (S,[3,1])
161val _ = (print "S(3,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
162
163
164val S  = RTensor.cat (SN1, SN2, 1)
165val _ = TensorFile.realTensorWrite (TextIO.stdOut) S
166
167val v = RTensor.sub (S,[0,0])
168val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
169val v = RTensor.sub (S,[0,1])
170val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
171val v = RTensor.sub (S,[0,2])
172val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
173val v = RTensor.sub (S,[1,0])
174val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
175val v = RTensor.sub (S,[1,1])
176val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
177val v = RTensor.sub (S,[1,2])
178val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
179val v = RTensor.sub (S,[2,0])
180val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
181val v = RTensor.sub (S,[2,1])
182val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
183val v = RTensor.sub (S,[2,2])
184val _ = (print "v = "; TensorFile.realWrite (TextIO.stdOut) v)
185
186val S1 = RTensorSlice.slice ([([0,0],[N-1,0])],S)
187val S2 = RTensorSlice.slice ([([0,1],[N-1,1])],S)
188val S3 = RTensorSlice.slice ([([0,2],[N-1,2])],S)
189
190val _ = TensorFile.realTensorSliceWrite (TextIO.stdOut) S1
191val _ = TensorFile.realTensorSliceWrite (TextIO.stdOut) S2
192val _ = TensorFile.realTensorSliceWrite (TextIO.stdOut) S3
193
194val _ = putStrLn (TextIO.stdOut, "insert:")
195val S    = RTensor.new ([4,4],0.0)
196val ones = RTensor.new ([2,2],1.0)
197val twos = RTensor.new ([2,2],2.0)
198val threes = RTensor.new ([1,2],3.0)
199val _    = TensorFile.realTensorWrite (TextIO.stdOut) threes
200val _    = RTensor.insert (S, threes, [2,0])
201val _    = RTensor.insert (S, ones, [0,0])
202val _    = RTensor.insert (S, twos, [2,2])
203val _    = TensorFile.realTensorWrite (TextIO.stdOut) S
204val v = RTensor.sub (S,[0,0])
205val _ = (print "S(0,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
206val v = RTensor.sub (S,[0,1])
207val _ = (print "S(0,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
208val v = RTensor.sub (S,[0,2])
209val _ = (print "S(0,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
210val v = RTensor.sub (S,[0,3])
211val _ = (print "S(0,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
212val v = RTensor.sub (S,[1,0])
213val _ = (print "S(1,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
214val v = RTensor.sub (S,[1,1])
215val _ = (print "S(1,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
216val v = RTensor.sub (S,[1,2])
217val _ = (print "S(1,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
218val v = RTensor.sub (S,[1,3])
219val _ = (print "S(1,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
220val v = RTensor.sub (S,[2,0])
221val _ = (print "S(2,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
222val v = RTensor.sub (S,[2,1])
223val _ = (print "S(2,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
224val v = RTensor.sub (S,[2,2])
225val _ = (print "S(2,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
226val v = RTensor.sub (S,[2,3])
227val _ = (print "S(2,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
228val v = RTensor.sub (S,[3,0])
229val _ = (print "S(3,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
230val v = RTensor.sub (S,[3,1])
231val _ = (print "S(3,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
232val v = RTensor.sub (S,[3,2])
233val _ = (print "S(3,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
234val v = RTensor.sub (S,[3,3])
235val _ = (print "S(3,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
236
237val r  = Range.fromto (RTensor.shape SN) ([0,0],[1,4])
238val xs = Range.foldi_range
239             (fn ((i,j),ax) => (Index.toInt (RTensor.shape SN) i,
240                                Index.toInt (RTensor.shape SN) j) :: ax)
241             [] r
242
243val _ = putStrLn (TextIO.stdOut, "insert:")
244val S    = RTensor.new ([4,4],0.0)
245val ones = RTensor.new ([4,2],1.0)
246val twos = RTensor.new ([4,2],2.0)
247val _    = RTensor.insert (S, ones, [0,0])
248val _    = RTensor.insert (S, twos, [0,2])
249val v = RTensor.sub (S,[0,0])
250val _ = (print "S(0,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
251val v = RTensor.sub (S,[0,1])
252val _ = (print "S(0,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
253val v = RTensor.sub (S,[0,2])
254val _ = (print "S(0,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
255val v = RTensor.sub (S,[0,3])
256val _ = (print "S(0,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
257val v = RTensor.sub (S,[1,0])
258val _ = (print "S(1,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
259val v = RTensor.sub (S,[1,1])
260val _ = (print "S(1,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
261val v = RTensor.sub (S,[1,2])
262val _ = (print "S(1,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
263val v = RTensor.sub (S,[1,3])
264val _ = (print "S(1,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
265val v = RTensor.sub (S,[2,0])
266val _ = (print "S(2,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
267val v = RTensor.sub (S,[2,1])
268val _ = (print "S(2,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
269val v = RTensor.sub (S,[2,2])
270val _ = (print "S(2,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
271val v = RTensor.sub (S,[2,3])
272val _ = (print "S(2,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
273val v = RTensor.sub (S,[3,0])
274val _ = (print "S(3,0) = "; TensorFile.realWrite (TextIO.stdOut) v)
275val v = RTensor.sub (S,[3,1])
276val _ = (print "S(3,1) = "; TensorFile.realWrite (TextIO.stdOut) v)
277val v = RTensor.sub (S,[3,2])
278val _ = (print "S(3,2) = "; TensorFile.realWrite (TextIO.stdOut) v)
279val v = RTensor.sub (S,[3,3])
280val _ = (print "S(3,3) = "; TensorFile.realWrite (TextIO.stdOut) v)
281
282
283fun fromDiag (m, n, a, dflt) =
284    if Index.validShape [m,n]
285    then 
286        (let 
287             val na   = RTensor.Array.length a
288             val na'  = na-1
289             val te = RTensor.new ([m,n], dflt)
290             fun diag (i, j, ia) =
291                 let
292                     val ia' = 
293                         (RTensor.update (te, [i,j], RTensor.Array.sub (a, ia));
294                          if ia = na' then 0 else ia+1)
295                 in
296                     if (i=0) orelse (j=0) 
297                     then te
298                     else diag (i-1, j-1, ia)
299                 end
300         in
301             diag (m-1, n-1, 0)
302         end)
303    else 
304        raise RTensor.Shape
305
306
307val diagtensor = fromDiag (4, 4, Real64Array.fromList [1.0, 2.0, 3.0], 0.0)
308
309val _  = TensorFile.realTensorWrite (TextIO.stdOut) diagtensor
310
311end
Note: See TracBrowser for help on using the repository browser.