File: t_Matrix_decomposition.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (75 lines) | stat: -rwxr-xr-x 1,971 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
#! /usr/bin/env python

import openturns as ot

ot.TESTPREAMBLE()


def quadM(m, n):
    res = ot.Matrix(m, n)
    for i in range(m):
        for j in range(n):
            res[i, j] = (i + 1.0) ** (j + 1.0)
    return res


def testQR(m, n, full, keep):
    matrix1 = quadM(m, n)
    print("M=", matrix1)
    if keep:
        Q, R = matrix1.computeQR(full)
    else:
        Q, R = matrix1.computeQRInPlace(full)
    print("full=", full, "keep=", keep)
    print("Q= ", Q)
    print("R=", R)
    print("Q*R=", Q * R)
    if keep:
        print("M2=", matrix1)


# Square case
matrix1 = quadM(3, 3)
matrix1.setName("matrix1")
print("matrix1 = ", repr(matrix1))

result1 = matrix1.computeSingularValues()
print("svd (svd only)= ", repr(result1))

result1, u, v = matrix1.computeSVD(True)
print("svd (svd + U, V full)= ", repr(result1))
result1, u, v = matrix1.computeSVD(False)
print("svd (svd + U, V small)= ", repr(result1), ", U=", repr(u), ", v=", repr(v))

# Rectangular case, m < n
matrix1 = quadM(3, 5)
matrix1.setName("matrix1")
print("matrix1 = ", repr(matrix1))

result1 = matrix1.computeSingularValues()
print("svd (svd only)= ", repr(result1))

result1, u, v = matrix1.computeSVD(True)
print("svd (svd + U, V full)= ", repr(result1))
result1, u, v = matrix1.computeSVD(False)
print("svd (svd + U, V small)= ", repr(result1), ", U=", repr(u), ", v=", repr(v))

# Rectangular case, m > n
matrix1 = quadM(5, 3)
matrix1.setName("matrix1")
print("matrix1 = ", repr(matrix1))

result1 = matrix1.computeSingularValues()
print("svd (svd only)= ", repr(result1))

result1, u, v = matrix1.computeSVD(True)
print("svd (svd + U, V full)= ", repr(result1))
# result1, u, v = matrix1.computeSVD(False)
# print "svd (svd + U, V small)= ", repr(result1), ", U=", repr(u), ",
# v=", repr(v)

for iFull in range(2):
    for iKeep in range(2):
        testQR(3, 3, iFull == 1, iKeep == 1)
        testQR(3, 5, iFull == 1, iKeep == 1)
        testQR(5, 3, iFull == 1, iKeep == 1)