File: t_Matrix_solve.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 (108 lines) | stat: -rwxr-xr-x 2,583 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
#! /usr/bin/env python

from __future__ import print_function
from openturns import *

TESTPREAMBLE()

try:
    # First : an n by n case
    matrix1 = Matrix(2, 2)
    matrix1[0, 0] = 1.0
    matrix1[1, 0] = 2.0
    matrix1[0, 1] = 5.0
    matrix1[1, 1] = 12.0
    print("matrix1=" + repr(matrix1))

    pt1 = NumericalPoint()
    pt1.add(5.0)
    pt1.add(0.0)
    print("pt1=" + repr(pt1))

    result1 = NumericalPoint()
    result1 = matrix1.solveLinearSystem(pt1)
    print("result1=" + repr(result1))

    # Second : an n by p case, n < p
    matrix2 = Matrix(2, 3)
    matrix2[0, 0] = 1.0
    matrix2[1, 0] = 2.0
    matrix2[0, 1] = 5.0
    matrix2[1, 1] = 12.0
    matrix2[0, 2] = 3.0
    matrix2[1, 2] = 4.0
    print("matrix2=" + repr(matrix2))

    pt2 = NumericalPoint()
    pt2.add(5.0)
    pt2.add(0.0)
    print("pt2=" + repr(pt2))

    result2 = NumericalPoint()
    result2 = matrix2.solveLinearSystem(pt2)
    print("result2=" + repr(result2))

    # Third : an n by p case, n > p
    matrix3 = Matrix(3, 2)
    matrix3[0, 0] = 1.0
    matrix3[1, 0] = 2.0
    matrix3[2, 0] = 4.0
    matrix3[0, 1] = 5.0
    matrix3[1, 1] = 12.0
    matrix3[2, 1] = 3.0

    print("matrix3=" + repr(matrix3))

    pt3 = NumericalPoint()
    pt3.add(5.0)
    pt3.add(0.0)
    pt3.add(1.0)
    print("pt3=" + repr(pt3))

    result3 = NumericalPoint()
    result3 = matrix3.solveLinearSystem(pt3, True)
    print("result3=" + repr(result3))

    b1 = Matrix(2, 4)
    b1[0, 0] = 5.0
    b1[1, 0] = 1.0
    b1[0, 1] = 10.0
    b1[1, 1] = 2.0
    b1[0, 2] = 15.0
    b1[1, 2] = 3.0
    b1[0, 3] = 20.0
    b1[1, 3] = 4.0
    print("b1=" + repr(b1))
    result4 = matrix1.solveLinearSystem(b1, True)
    print("result4=" + repr(result4))
    result4 = matrix1.solveLinearSystem(b1, False)
    print("result4=" + repr(result4))

    result5 = matrix2.solveLinearSystem(b1, True)
    print("result5=" + repr(result5))
    result5 = matrix2.solveLinearSystem(b1, False)
    print("result5=" + repr(result5))

    b2 = Matrix(3, 4)
    b2[0, 0] = 5.0
    b2[1, 0] = 1.0
    b2[2, 0] = -2.0
    b2[0, 1] = 10.0
    b2[1, 1] = 2.0
    b2[2, 1] = -4.0
    b2[0, 2] = 15.0
    b2[1, 2] = 3.0
    b2[2, 2] = -6.0
    b2[0, 3] = 20.0
    b2[1, 3] = 4.0
    b2[2, 3] = -8.0
    print("b2=" + repr(b2))
    result6 = matrix3.solveLinearSystem(b2, True)
    print("result6=" + repr(result6))
    result6 = matrix3.solveLinearSystem(b2, False)
    print("result6 = " + repr(result6))

except:
    import sys
    print("t_MatrixSolveLinearSystem_std.py",
          sys.exc_info()[0], sys.exc_info()[1])