Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/modules/misc.py
4036 views
1
"""
2
Miscellaneous module-related functions.
3
4
AUTHORS:
5
-- William Stein (2007-11-18)
6
"""
7
8
####################################################################################
9
# Copyright (C) 2007 William Stein <[email protected]>
10
# Distributed under the terms of the GNU General Public License (GPL)
11
# The full text of the GPL is available at:
12
# http://www.gnu.org/licenses/
13
####################################################################################
14
15
from sage.matrix.constructor import matrix
16
from sage.rings.integer_ring import ZZ
17
18
# Function below could be replicated into
19
# sage.matrix.matrix_integer_dense.Matrix_integer_dense.is_LLL_reduced
20
# which is its only current use (2011-02-26). Then this could
21
# be deprecated and this file removed.
22
23
def gram_schmidt(B):
24
r"""
25
Return the Gram-Schmidt orthogonalization of the entries in the list
26
B of vectors, along with the matrix mu of Gram-Schmidt coefficients.
27
28
Note that the output vectors need not have unit length. We do this
29
to avoid having to extract square roots.
30
31
.. note::
32
33
Use of this function is discouraged. It fails on linearly
34
dependent input and its output format is not as natural as it
35
could be. Instead, see :meth:`sage.matrix.matrix2.Matrix2.gram_schmidt`
36
which is safer and more general-purpose.
37
38
EXAMPLES:
39
40
sage: B = [vector([1,2,1/5]), vector([1,2,3]), vector([-1,0,0])]
41
sage: from sage.modules.misc import gram_schmidt
42
sage: G, mu = gram_schmidt(B)
43
sage: G
44
[(1, 2, 1/5), (-1/9, -2/9, 25/9), (-4/5, 2/5, 0)]
45
sage: G[0] * G[1]
46
0
47
sage: G[0] * G[2]
48
0
49
sage: G[1] * G[2]
50
0
51
sage: mu
52
[ 0 0 0]
53
[ 10/9 0 0]
54
[-25/126 1/70 0]
55
sage: a = matrix([])
56
sage: a.gram_schmidt()
57
([], [])
58
sage: a = matrix([[],[],[],[]])
59
sage: a.gram_schmidt()
60
([], [])
61
62
Linearly dependent input leads to a zero dot product in a denominator.
63
This shows that Trac #10791 is fixed. ::
64
65
sage: from sage.modules.misc import gram_schmidt
66
sage: V = [vector(ZZ,[1,1]), vector(ZZ,[2,2]), vector(ZZ,[1,2])]
67
sage: gram_schmidt(V)
68
Traceback (most recent call last):
69
...
70
ValueError: linearly dependent input for module version of Gram-Schmidt
71
"""
72
import sage.modules.free_module_element
73
if len(B) == 0 or len(B[0]) == 0:
74
return B, matrix(ZZ,0,0,[])
75
n = len(B)
76
Bstar = [B[0]]
77
K = B[0].base_ring().fraction_field()
78
zero = sage.modules.free_module_element.vector(K, len(B[0]))
79
if Bstar[0] == zero:
80
raise ValueError("linearly dependent input for module version of Gram-Schmidt")
81
mu = matrix(K, n, n)
82
for i in range(1,n):
83
for j in range(i):
84
mu[i,j] = B[i].dot_product(Bstar[j]) / (Bstar[j].dot_product(Bstar[j]))
85
Bstar.append(B[i] - sum(mu[i,j]*Bstar[j] for j in range(i)))
86
if Bstar[i] == zero:
87
raise ValueError("linearly dependent input for module version of Gram-Schmidt")
88
return Bstar, mu
89
90
91