# Gram-Schmidt with arbitrary inner product?

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Hi,

I'd like to find an orthonormal basis of polynomials using an arbitrary inner product. I know how to do this in Maxima:

ip(f, g) := integrate(f * g, x, -1, 1); /* for example */
y : gramschmidt([1, x, x^2], ip);


But is there a nice way to do this in sage?

Thanks! -Andrew

edit retag close merge delete

Sort by » oldest newest most voted

In sage, there is a method gram_schnidt associated to a matrix.

After the matrix is declared, the method can be immediately called. The following defines a matrix A , computes the Gram-Schnidt matrices G.M as in the examples from

A.gram_schmidt?


and does a check for the relation $A=MG$, it is a possible starting point:

var( 'x' )
def ip(f, g):
return integrate( f * g, x, -1, 1 )

f_list = [1, x, x^2, x^3, x^4]
A = matrix( QQ, len(f_list),
[ ip( f, g ) for f in f_list for g in f_list ] )

print "A is\n%s\n" % A

G, M = A.gram_schmidt()

print "G is\n%s\n" % G
print "M is\n%s\n" % M

print "Is MG = A? %s" % bool( M*G == A )


Results:

A is
[  2   0 2/3   0 2/5]
[  0 2/3   0 2/5   0]
[2/3   0 2/5   0 2/7]
[  0 2/5   0 2/7   0]
[2/5   0 2/7   0 2/9]

G is
[         2          0        2/3          0        2/5]
[         0        2/3          0        2/5          0]
[ -424/5439          0  1376/9065          0   248/1813]
[         0    -12/595          0      4/119          0]
[ 64/112035          0 -128/22407          0    64/9603]

M is
[       1        0        0        0        0]
[       0        1        0        0        0]
[675/1813        0        1        0        0]
[       0   75/119        0        1        0]
[425/1813        0 950/1067        0        1]

Is MG = A? True

more