# How to show that two given functions are linearly dependent

Is there a way with sage to show if two given functions are linearly dependent? Like if I have

f(x) = 17, g(x) = cos^2(x), h(x) = cos(2x)


Then sage could either give me the constants that would show they are linearly dependent, or say they aren't

edit retag close merge delete

Sort by » oldest newest most voted Let $f_1,\ldots,f_n$ be functions defined on a given interval $I$ and $n-1$ times differentiable. Let $W(f_1,\ldots,f_n)$ be the Wronskian of these functions. It is well known that:

if $f_1,\ldots,f_n$ are linearly dependent on $I$, then $W(f_1,\ldots,f_n)$ vanishes on $I$.

In general, the converse is not true: the vanishing of the Wronskian on $I$ does not imply that the functions are linearly dependent. However, if $f_1,\ldots,f_n$ are analytic, then these functions are linearly dependent on $I$ if and only if $W(f_1,\ldots,f_n)\equiv 0$ on $I$.

Since $f(x)=17$, $g(x)=\cos^2(x)$ and $h(x)=\cos(2x)$ are analytic on $\mathbb{R}$, we can check if they are linearly dependent by testing their Wronskian:

sage: f(x) = 17
sage: g(x) = cos(x)^2
sage: h(x) = cos(2*x)
sage: funs = [f(x),g(x),h(x)]
sage: bool(wronskian(*funs,x)==0)
True


Thus, $f$, $g$ and $h$ are linearly dependent.

more

First an answer for the special case, then a more general answer.

## Trigonometric functions

In the special case of the example in the question, this helps:

sage: f(x) = 17
sage: g(x) = cos(x)^2
sage: h(x) = cos(2*x)

sage: h(x).simplify_trig()
2*cos(x)^2 - 1


## Linear independence of functions

Returning to the general question, one way to check for possible linear dependence relations, or for proofs of independence, would be to project to a finite-dimensional subspace and check there.

Form a vectors from the value of each function at a chosen collection of evaluation points.

If these vectors are linearly independent, then the functions are.

If there is a linear relation between these vectors, then it's worth checking if the linear relation in fact holds for the functions themselves.

Let us illustrate with the three functions above.

Choose a collection of evaluation points:

sage: xx = (0, pi/6, pi/4, pi/3, pi/2)


Form a vector for each function:

sage: u = vector([f(x) for x in xx])
sage: v = vector([g(x) for x in xx])
sage: w = vector([h(x) for x in xx])


To look for dependence relations, compute the left kernel of the matrix with these vectors as rows.

sage: m = matrix([u, v, w])
sage: K = m.kernel()
sage: K
Vector space of degree 3 and dimension 1 over Algebraic Real Field
Basis matrix:
[  1 -34  17]


This suggests that maybe f - 34*g + 17*h is zero.

Extract the coefficients:

sage: a, b, c = K.basis()
sage: a, b, c
(1, -34, 17)


Check if the linear relation in fact holds for the functions:

sage: bool(a*f(x) + b*g(x) + c*h(x) == 0)
True

more

Hi again! I tried using the method you showed, but instead of getting a clean answer, I get "unable to convert pi to an element of Algebraic Real Field"

using

 f(x)=6*x
g(x)=2*x^2
h(x)=6*x-5*x^2
xx=(0,pi/6,pi/4,pi/3,pi/2)
u=vector(AA,[f(x) for x in xx])
v=vector(AA,[g(x) for x in xx])
w=vector(AA,[h(x) for x in xx])
m=matrix([u,v,w])
K=m.kernel()
show(K)


Any thoughts on if I did the code wrong?

1

You did nothing wrong.

Choosing xx = (0, pi/6, pi/4, pi/3, pi/2) gave algebraic values for the cosines so my original answer hardcoded constructing vectors over the algebraic numbers.

Using vector([...]) (as in edited answer) instead of vector(AA, [...]) (as in original answer) works both for original example and new one.

Is there a way to have it show a clean answer, like in your first reply to my question instead of something like

Vector space of degree 3 and dimension 1 over Symbolic Ring
Basis matrix:
[                            1 -15/2/(pi + (7*pi - pi^2)/pi)    -5/(pi + (7*pi - pi^2)/pi)]


I know that might be one answer but it's not helpful when finding non-trivial linear combinations lol

For three functions that are polynomials with integer coefficients, xx = (0, 1, 2, 3, 4, 5) might be a more natural choice of evaluation points.