File: t_Matrix_numpy.py

package info (click to toggle)
openturns 1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,588 kB
  • ctags: 26,495
  • sloc: cpp: 144,032; python: 26,855; ansic: 7,868; sh: 419; makefile: 263; yacc: 123; lex: 44
file content (391 lines) | stat: -rwxr-xr-x 11,607 bytes parent folder | download
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#! /usr/bin/env python

from __future__ import print_function
from openturns import *
import numpy as np

TESTPREAMBLE()

try:

    # Check tuple / NumericalPoint conversion
    t0 = (0.5, 1.5)
    p0 = NumericalPoint(t0)
    print("tuple", t0, "=> NumericalPoint", p0)

    t1 = tuple(p0)
    print("NumericalPoint", p0, "=> tuple", t1)

    print("NumericalPoint", p0, "+ tuple", t0, "=> NumericalPoint", p0 + t0)

    # Check list / NumericalPoint conversion
    l0 = [0.5, 1.5]
    p0 = NumericalPoint(l0)
    print("list", l0, "=> NumericalPoint", p0)

    l1 = list(p0)
    print("NumericalPoint", p0, "=> list", l1)

    print("NumericalPoint", p0, "+ list", l0, "=> NumericalPoint", p0 + l0)

    # Check sequence protocol for NumericalPoint
    for x in p0:
        print("value", x)
    x0 = p0[0]
    p0[0] = x0
    if x0 not in p0:
        raise ValueError("NumericalPoint badly implements __contains__()")

    # Check array / NumericalPoint conversion
    a0 = np.array((0.5, 1.5))
    p0 = NumericalPoint(a0)
    print("array", a0, "=> NumericalPoint", p0)

    a1 = np.array(p0)
    print("NumericalPoint", p0, "=> array", a1)

    print("NumericalPoint", p0, "+ array", a0, "=> NumericalPoint", p0 + a0)
    print("array", a0, "+ NumericalPoint", p0, "=> array", a0 + p0)

    # See ticket #423
    m0 = np.array([[1, 1], [2, 2], [3, 3]])
    try:
        p0 = NumericalPoint(m0)
        print(p0)
    except:
        print(
            'Conversion from 2d-array => NumericalPoint failed (as expected :)')

    # Check tuple / NumericalSample conversion
    t0 = ((1., 2.), (3., 4.))
    s0 = NumericalSample(t0)
    print("tuple", t0, "=> NumericalSample", s0)

    t0 = ([1., 2.], [3., 4.])
    s0 = NumericalSample(t0)
    print("tuple", t0, "=> NumericalSample", s0)

    t1 = tuple(s0)
    print("NumericalSample", s0, "=> tuple", t1)

    # Check list / NumericalSample conversion
    l0 = [[1., 2.], [3., 4.]]
    s0 = NumericalSample(l0)
    print("list", l0, "=> NumericalSample", s0)

    l0 = [(1., 2.), (3., 4.)]
    s0 = NumericalSample(l0)
    print("list", l0, "=> NumericalSample", s0)

    l1 = list(s0)
    print("NumericalSample", s0, "=> list", l1)

    # Check array / NumericalSample conversion
    a0 = np.array(((1., 2.), (3., 4.)))
    s0 = NumericalSample(a0)
    print("array", a0, "=> NumericalSample", s0)

    a1 = np.array(s0)
    print("NumericalSample", s0, "=> array", a1)

    # Check tuple / NumericalMathFunction interoperability
    F = NumericalMathFunction(
        ('E', 'F', 'L', 'I'), ('d',), ('-F*L^3/(3.*E*I)',))

    t0 = (1., 2., 3., 4.)
    print("NumericalPoint", F(t0), "= F( tuple", t0, ")")

    # Check list / NumericalMathFunction interoperability
    l0 = [1., 2., 3., 4.]
    print("NumericalPoint", F(l0), "= F( list", l0, ")")

    # Check array / NumericalMathFunction interoperability
    a0 = np.array((1., 2., 3., 4.))
    print("NumericalPoint", F(a0), "= F( array", a0, ")")
    a1 = np.array(((1., 2., 3., 4.), (5., 6., 7., 8.)))
    print("NumericalSample", F(a1), "= F( array", a1, ")")

    # Check Python function / NumericalMathFunction interoperability
    def aFunc(x):
        return [x[0] + x[1] + x[2] + x[3]]

    PYNMF = PythonFunction(4, 1, aFunc)

    print("NumericalPoint", PYNMF(t0), "= PYNMF( tuple", t0, ")")
    print("NumericalPoint", PYNMF(l0), "= PYNMF( list",  l0, ")")
    print("NumericalPoint", PYNMF(a0), "= PYNMF( array", a0, ")")
    print("NumericalSample", PYNMF(a1), "= PYNMF( array", a1, ")")

    # Check 2-d array which nested dim is size=1 / NumericalMathFunction
    # interoperability
    def aFunc2(x):
        return [2.0 * x[0]]

    PYNMF = PythonFunction(1, 1, aFunc2)

    a0 = np.array(([1.]))
    print("NumericalPoint", PYNMF(a0), "= PYNMF( array", a0, ")")
    a1 = np.array(([1.], [2.], [3.]))
    print("NumericalSample", PYNMF(a1), "= PYNMF( array", a1, ")")

    # Check tuple / Indices conversion
    t0 = (1, 2)
    i0 = Indices(t0)
    print("tuple", t0, "=> Indices", i0)

    t1 = tuple(i0)
    print("Indices", i0, "=> tuple", tuple([int(x) for x in t0]))

    # Check list / Indices conversion
    l0 = [3, 4, 5]
    i0 = Indices(l0)
    print("list", l0, "=> Indices", i0)

    l1 = list(i0)
    print("Indices", i0, "=> list", [int(x) for x in l1])

    # check Indices typemap
    sample = Normal(3).getSample(10)
    print(sample.getDescription())
    marginal = sample.getMarginal((0, 2))
    print(marginal.getDescription())
    marginal = sample.getMarginal([0, 2])
    print(marginal.getDescription())

    # Check tuple / Description conversion
    t0 = ('blob', 'zou')
    i0 = Description(t0)
    print("tuple", t0, "=> Description", i0)

    t1 = tuple(i0)
    print("Description", i0, "=> tuple", t1)

    # Check list / Description conversion
    l0 = ['blob', 'zou']
    i0 = Description(l0)
    print("list", l0, "=> Description", i0)

    l1 = list(i0)
    print("Description", i0, "=> list", l1)

    # array / Description conversion
    a0 = np.array(('x0', 'x1', 'x2'))
    d0 = Description(a0)
    print("array", a0, "=> Description", d0)

    a1 = np.array(d0)
    print("Description", d0, "=> array", a1)

    # check Description typemap
    sample.setDescription(('x0', 'x1', 'x2'))
    print(sample.getDescription())
    sample.setDescription(('y0', 'y1', 'y2'))
    print(sample.getDescription())
    sample.setDescription(np.array(('z0', 'z1', 'z2')))
    print(sample.getDescription())

    # Check Matrix tuple constructor
    t0 = (1., 2., 3., 4.)

    m0 = Matrix(2, 2, t0)
    print("tuple", t0, "=> Matrix", m0)

    m0 = SquareMatrix(2, t0)
    print("tuple", t0, "=> SquareMatrix", m0)

    m0 = SymmetricMatrix(2, t0)
    print("tuple", t0, "=> SymmetricMatrix", m0)

    m0 = Tensor(2, 2, 1, t0)
    print("tuple", t0, "=> Tensor", m0)

    m0 = SymmetricTensor(2, 1, t0)
    print("tuple", t0, "=> SymmetricTensor", m0)

    m0 = CorrelationMatrix(2, t0)
    print("tuple", t0, "=> CorrelationMatrix", m0)

    m0 = CovarianceMatrix(2, t0)
    print("tuple", t0, "=> CovarianceMatrix", m0)

    t0c = (1. + 3.j, 2. - 5.j, 3. + 7.j, 4. - 9.j)

    m0 = ComplexMatrix(2, 2, t0)
    print("tuple", t0, "=> ComplexMatrix", m0)

    m0 = ComplexMatrix(2, 2, t0c)
    print("tuple", t0c, "=> ComplexMatrix", m0)

    m0 = SquareComplexMatrix(2, t0)
    print("tuple", t0, "=> SquareComplexMatrix", m0)

    m0 = SquareComplexMatrix(2, t0c)
    print("tuple", t0c, "=> SquareComplexMatrix", m0)

    # Check Matrix list constructor
    l0 = [1., 2., 3., 4.]
    m0 = Matrix(2, 2, l0)
    print("list", l0, "=> Matrix", m0)

    m0 = SquareMatrix(2, l0)
    print("list", l0, "=> SquareMatrix", m0)

    m0 = SymmetricMatrix(2, l0)
    print("list", l0, "=> SymmetricMatrix", m0)

    m0 = Tensor(2, 2, 1, l0)
    print("list", l0, "=> Tensor", m0)

    m0 = SymmetricTensor(2, 1, l0)
    print("list", l0, "=> SymmetricTensor", m0)

    m0 = CorrelationMatrix(2, l0)
    print("list", l0, "=> CorrelationMatrix", m0)

    m0 = CovarianceMatrix(2, l0)
    print("list", l0, "=> CovarianceMatrix", m0)

    l0c = [1. + 3.j, 2. - 5.j, 3. + 7.j, 4. - 9.j]

    m0 = ComplexMatrix(2, 2, l0)
    print("list", l0, "=> ComplexMatrix", m0)

    m0 = ComplexMatrix(2, 2, l0c)
    print("list", l0c, "=> ComplexMatrix", m0)

    m0 = SquareComplexMatrix(2, l0)
    print("list", l0, "=> SquareComplexMatrix", m0)

    m0 = SquareComplexMatrix(2, l0c)
    print("list", l0c, "=> SquareComplexMatrix", m0)

    # Check Matrix 1-d array constructor
    a0 = np.array((1., 2., 3., 4.))
    m0 = Matrix(2, 2, a0)
    print("array", a0, "=> Matrix", m0)

    m0 = SquareMatrix(2, a0)
    print("array", a0, "=> SquareMatrix", m0)

    m0 = SymmetricMatrix(2, a0)
    print("array", a0, "=> SymmetricMatrix", m0)

    m0 = Tensor(2, 2, 1, a0)
    print("array", a0, "=> Tensor", m0)

    m0 = SymmetricTensor(2, 1, a0)
    print("array", a0, "=> SymmetricTensor", m0)

    m0 = CorrelationMatrix(2, a0)
    print("array", a0, "=> CorrelationMatrix", m0)

    m0 = CovarianceMatrix(2, a0)
    print("array", a0, "=> CovarianceMatrix", m0)

    a0c = np.array((1. + 3j, 2. - 5j, 3. - 7j, 4. + 9j))

    m0 = ComplexMatrix(2, 2, a0)
    print("array", a0, "=> ComplexMatrix", m0)

    m0 = ComplexMatrix(2, 2, a0c)
    print("array", a0c, "=> ComplexMatrix", m0)

    m0 = SquareComplexMatrix(2, a0)
    print("array", a0, "=> SquareComplexMatrix", m0)

    m0 = SquareComplexMatrix(2, a0c)
    print("array", a0c, "=> SquareComplexMatrix", m0)

    # check array / Matrix conversion
    a0 = np.array(((1., 2.), (3., 4.), (5., 6.)))
    m0 = Matrix(a0)
    print("array", a0, "=> Matrix", m0)
    a1 = np.array(m0)
    print("Matrix", m0, "=> array", a1)

    a0 = np.array(((1., 2.), (3., 4.)))
    m0 = SquareMatrix(a0)
    print("array", a0, "=> SquareMatrix", m0)
    a1 = np.array(m0)
    print("SquareMatrix", m0, "=> array", a1)

    a0 = np.array(((1., 2.), (0., 4.)))
    m0 = TriangularMatrix(a0)
    print("array", a0, "=> TriangularMatrix", m0)
    a1 = np.array(m0)
    print("TriangularMatrix", m0, "=> array", a1)

    a0 = np.array(((1., 2.), (2., 4.)))
    m0 = SymmetricMatrix(a0)
    print("array", a0, "=> SymmetricMatrix", m0)
    a1 = np.array(m0)
    print("SymmetricMatrix", m0, "=> array", a1)

    a0 = np.array(
        (((1., 2.), (3., 4.), (5., 6.)), ((7., 8.), (9., 10.), (11., 12.)),
         ((13., 14.), (15., 16.), (17., 18.)), ((19., 20.), (21., 22.), (23., 24.))))
    m0 = Tensor(a0)
    print("array", a0, "=> Tensor", m0)
    a1 = np.array(m0)
    print("Tensor", m0, "=> array", a1)

    a0 = np.array((((1., 2.), (3., 4.)), ((3., 4.), (7., 8.))))
    m0 = SymmetricTensor(a0)
    print("array", a0, "=> SymmetricTensor", m0)
    a1 = np.array(m0)
    print("SymmetricTensor", m0, "=> array", a1)

    a0 = np.array(((2., 1.), (1., 2.)))
    m0 = CovarianceMatrix(a0)
    print("array", a0, "=> CovarianceMatrix", m0)
    a1 = np.array(m0)
    print("CovarianceMatrix", m0, "=> array", a1)

    a0 = np.array(((1., 0.5), (0.5, 1.)))
    m0 = CorrelationMatrix(a0)
    print("array", a0, "=> CorrelationMatrix", m0)
    a1 = np.array(m0)
    print("CorrelationMatrix", m0, "=> array", a1)

    a0 = np.array(((1. + 3j, 2. - 5j, 3. + 7j), (4. - 9j, 5. + 11j, 6. - 13j)))
    m0 = ComplexMatrix(a0)
    print("array", a0, "=> ComplexMatrix", m0)
    a1 = np.array(m0)
    print("ComplexMatrix", m0, "=> array", a1)

    a0 = np.array(((1. + 3j, 2. - 5j), (3. + 7j, 4. - 9j)))
    m0 = SquareComplexMatrix(a0)
    print("array", a0, "=> SquareComplexMatrix", m0)
    a1 = np.array(m0)
    print("SquareComplexMatrix", m0, "=> array", a1)

    a0 = np.array(((1. + 3j, 0.), (3. + 7j, 4. - 9j)))
    m0 = TriangularComplexMatrix(a0)
    print("array", a0, "=> TriangularComplexMatrix", m0)
    a1 = np.array(m0)
    print("TriangularComplexMatrix", m0, "=> array", a1)

    a0 = np.array(((1. + 3j, 2. - 5j), (2. + 5j, 4. - 9j)))
    m0 = HermitianMatrix(a0)
    print("array", a0, "=> HermitianMatrix", m0)
    a1 = np.array(m0)
    print("HermitianMatrix", m0, "=> array", a1)

    # check np.matrix / Matrix conversion
    a0 = np.matrix(((1., 2.), (3., 4.), (5., 6.)))
    m0 = Matrix(a0)
    print("matrix", a0, "=> Matrix", m0)
    a1 = np.array(m0)
    print("Matrix", m0, "=> matrix", a1)

    a0 = np.matrix(
        ((1. + 3j, 2. - 5j, 3. + 7j), (4. - 9j, 5. + 11j, 6. - 13j)))
    m0 = ComplexMatrix(a0)
    print("matrix", a0, "=> ComplexMatrix", m0)
    a1 = np.array(m0)
    print("ComplexMatrix", m0, "=> matrix", a1)

except:
    import sys
    import traceback
    traceback.print_exc(file=sys.stdout)