File: t_Matrix_std.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 (183 lines) | stat: -rwxr-xr-x 5,482 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
#! /usr/bin/env python

from __future__ import print_function
from openturns import *
from math import *

TESTPREAMBLE()

try:
    # TEST NUMBER ZERO : DEFAULT CONSTRUCTOR AND STRING CONVERTER
    print("test number zero : default constructor and string converter")

    # Default constructor
    matrix0 = Matrix()

    # String converter
    print("matrix0 = ", repr(matrix0))

    # TEST NUMBER ONE : CONSTRUCTOR WITH SIZE, OPERATOR() AND STRING CONVERTER
    print(
        "test number one : constructor with size, operator() and string converter")

    # Constructor with size
    matrix1 = Matrix(2, 2)

    # Check operator() methods
    matrix1[0, 0] = 1.
    matrix1[1, 0] = 2.
    matrix1[0, 1] = 3.
    matrix1[1, 1] = 4.

    # String converter
    print("matrix1 = ", repr(matrix1))

    # TEST NUMBER TWO : COPY CONSTRUCTOR AND STRING CONVERTER
    print("test number two : copy constructor and string converter")

    # Copy constructor
    matrix2 = Matrix(matrix1)

    # String converter
    print("matrix2 = ", repr(matrix2))

    # TEST NUMBER THREE : GET DIMENSIONS METHODS
    print("test number three : get dimensions methods")

    # Get dimension methods
    print("matrix1's nbRows = ", matrix1.getNbRows())
    print("matrix1's nbColumns = ", matrix1.getNbColumns())

    # TEST NUMBER FOUR : CONSTRUCTOR WITH COLLECTION
    print("test number four : constructor with collection method")

    # Create the collection of values
    elementsValues = NumericalScalarCollection()
    elementsValues.add(1.)
    elementsValues.add(2.)
    elementsValues.add(3.)
    elementsValues.add(4.)
    elementsValues.add(5.)
    elementsValues.add(6.)

    # Check the content of the collection
    print("elementsValues = ", repr(elementsValues))

    # Check the constructor with collection
    matrix0bis = Matrix(2, 2, elementsValues)
    print("matrix0bis = ", repr(matrix0bis))

    # TEST NUMBER FIVE : ASSIGNMENT METHOD
    print("test number five : assignment method")

    # Assignment method
    # No sense with pyton

    # TEST NUMBER SIX : TRANSPOSITION METHOD
    print("test number six : transposition method")

    # Check transpose method
    matrix4 = matrix1.transpose()
    print("matrix1 transposed = ", repr(matrix4))

    # TEST NUMBER SEVEN : ADDITION METHOD
    print("test number seven : addition method")

    # Check addition method : we check the operator and the symmetry of the
    # operator, thus testing the comparison operator
    sum1 = matrix1 + matrix4
    sum2 = matrix4 + matrix1
    print("sum1 = ", repr(sum1))
    print("sum2 = ", repr(sum2))
    print("sum1 equals sum2 = ", sum1 == sum2)

    # TEST NUMBER EIGHT : SUBSTRACTION METHOD
    print("test number eight : substraction method")

    # Check substraction method
    diff = matrix1 - matrix4
    print("diff = ", repr(diff))

    # TEST NUMBER NINE : MATRIX MULTIPLICATION METHOD
    print("test number nine : matrix multiplication method")

    # Check multiplication method
    prod = matrix1 * matrix4
    print("prod = ", repr(prod))

    # TEST NUMBER TEN : MULTIPLICATION WITH A NUMERICAL POINT METHOD
    print("test number ten : multiplication with a numerical point method")

    # Create the numerical point
    pt = NumericalPoint()
    pt.add(1.)
    pt.add(2.)
    print("pt = ", repr(pt))

    # Check the product method
    ptResult = matrix1 * pt
    print("ptResult = ", repr(ptResult))

    # TEST NUMBER ELEVEN : MULTIPLICATION AND DIVISION BY A NUMERICAL SCALAR
    # METHODS
    print(
        "test number eleven : multiplication and division by a numerical scalar methods")

    # Check the multiplication method
    s = 3.
    scalprod1 = matrix1 * s
    # bug PYTHON scalprod2 = s * matrix1
    scalprod3 = matrix1 * s
    print("scalprod1 = ", repr(scalprod1))
    # print  "scalprod2 = " , scalprod2
    print("scalprod3 = ", repr(scalprod3))
    # print  "scalprod1 equals scalprod2 = " , (scalprod1 == scalprod2)
    print("scalprod1 equals scalprod3 = ", (scalprod1 == scalprod3))
    # print  "scalprod2 equals scalprod3 = " , (scalprod2 == scalprod3)

    # Check the division method
    scaldiv1 = matrix1 / s
    scaldiv2 = matrix1 / s
    print("scaldiv1 = ", repr(scaldiv1))
    print("scaldiv2 = ", repr(scaldiv2))
    print("scaldiv1 equals scaldiv2 = ", (scaldiv1 == scaldiv2))

    # TEST NUMBER TWELVE : ISEMPTY METHOD
    print("test number twelve : isEmpty method")

    # Check method isEmpty
    matrix5 = Matrix()
    matrix6 = Matrix()
    # matrix6.setDimensions(0,3)
    print("matrix1 is empty = ", matrix1.isEmpty())
    print("matrix5 is empty = ", matrix5.isEmpty())
    print("matrix6 is empty = ", matrix6.isEmpty())
    print("matrix0 is empty = ", matrix0.isEmpty())

    # TEST NUMBER FOURTEEN : MULTIPLICATION WITH A NUMERICAL POINT METHOD
    print(
        "test number fourteen : multiplication with a numerical point method")

    # Create the numerical point
    pt_test = NumericalPoint()
    pt_test.add(1.)
    pt_test.add(2.)
    print("pt_test = ", repr(pt_test))

    A = Matrix(2, 2)
    A[0, 0] = 0.5
    A[1, 0] = -(sqrt(3.) / 2)
    A[0, 1] = (sqrt(3.) / 2)
    A[1, 1] = 0.5
    B = A.transpose()
    id = B * A

    # Check the product method
    ptResult2 = id * pt_test
    print("A = ", repr(A))
    print("B = ", repr(B))
    print("id = ", repr(id))
    print("ptResult2 = ", repr(ptResult2))
except:
    import sys
    print("t_Matrix_std.py", sys.exc_info()[0], sys.exc_info()[1])