ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 27 Mar 2021 12:34:15 +0100How to show that two given functions are linearly dependenthttps://ask.sagemath.org/question/56390/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'tFri, 26 Mar 2021 17:36:04 +0100https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/Answer by slelievre for <p>Is there a way with sage to show if two given functions are linearly dependent?
Like if I have </p>
<pre><code>f(x) = 17, g(x) = cos^2(x), h(x) = cos(2x)
</code></pre>
<p>Then sage could either give me the constants that would show they are linearly dependent, or say they aren't</p>
https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?answer=56391#post-id-56391First 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()[0]
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)
TrueFri, 26 Mar 2021 18:29:21 +0100https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?answer=56391#post-id-56391Comment by slelievre for <p>First an answer for the special case, then a more general answer.</p>
<h2>Trigonometric functions</h2>
<p>In the special case of the example in the question, this helps:</p>
<pre><code>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
</code></pre>
<h2>Linear independence of functions</h2>
<p>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.</p>
<p>Form a vectors from the value of each function at
a chosen collection of evaluation points.</p>
<p>If these vectors are linearly independent, then the functions are.</p>
<p>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.</p>
<p>Let us illustrate with the three functions above.</p>
<p>Choose a collection of evaluation points:</p>
<pre><code>sage: xx = (0, pi/6, pi/4, pi/3, pi/2)
</code></pre>
<p>Form a vector for each function:</p>
<pre><code>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])
</code></pre>
<p>To look for dependence relations, compute the left kernel
of the matrix with these vectors as rows.</p>
<pre><code>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]
</code></pre>
<p>This suggests that maybe <code>f - 34*g + 17*h</code> is zero.</p>
<p>Extract the coefficients:</p>
<pre><code>sage: a, b, c = K.basis()[0]
sage: a, b, c
(1, -34, 17)
</code></pre>
<p>Check if the linear relation in fact holds for the functions:</p>
<pre><code>sage: bool(a*f(x) + b*g(x) + c*h(x) == 0)
True
</code></pre>
https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56407#post-id-56407For three functions that are polynomials with integer coefficients, `xx = (0, 1, 2, 3, 4, 5)` might be a more natural choice of evaluation points.Sat, 27 Mar 2021 12:34:15 +0100https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56407#post-id-56407Comment by Presto.creator for <p>First an answer for the special case, then a more general answer.</p>
<h2>Trigonometric functions</h2>
<p>In the special case of the example in the question, this helps:</p>
<pre><code>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
</code></pre>
<h2>Linear independence of functions</h2>
<p>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.</p>
<p>Form a vectors from the value of each function at
a chosen collection of evaluation points.</p>
<p>If these vectors are linearly independent, then the functions are.</p>
<p>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.</p>
<p>Let us illustrate with the three functions above.</p>
<p>Choose a collection of evaluation points:</p>
<pre><code>sage: xx = (0, pi/6, pi/4, pi/3, pi/2)
</code></pre>
<p>Form a vector for each function:</p>
<pre><code>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])
</code></pre>
<p>To look for dependence relations, compute the left kernel
of the matrix with these vectors as rows.</p>
<pre><code>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]
</code></pre>
<p>This suggests that maybe <code>f - 34*g + 17*h</code> is zero.</p>
<p>Extract the coefficients:</p>
<pre><code>sage: a, b, c = K.basis()[0]
sage: a, b, c
(1, -34, 17)
</code></pre>
<p>Check if the linear relation in fact holds for the functions:</p>
<pre><code>sage: bool(a*f(x) + b*g(x) + c*h(x) == 0)
True
</code></pre>
https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56403#post-id-56403Is 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 lolSat, 27 Mar 2021 01:02:05 +0100https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56403#post-id-56403Comment by slelievre for <p>First an answer for the special case, then a more general answer.</p>
<h2>Trigonometric functions</h2>
<p>In the special case of the example in the question, this helps:</p>
<pre><code>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
</code></pre>
<h2>Linear independence of functions</h2>
<p>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.</p>
<p>Form a vectors from the value of each function at
a chosen collection of evaluation points.</p>
<p>If these vectors are linearly independent, then the functions are.</p>
<p>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.</p>
<p>Let us illustrate with the three functions above.</p>
<p>Choose a collection of evaluation points:</p>
<pre><code>sage: xx = (0, pi/6, pi/4, pi/3, pi/2)
</code></pre>
<p>Form a vector for each function:</p>
<pre><code>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])
</code></pre>
<p>To look for dependence relations, compute the left kernel
of the matrix with these vectors as rows.</p>
<pre><code>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]
</code></pre>
<p>This suggests that maybe <code>f - 34*g + 17*h</code> is zero.</p>
<p>Extract the coefficients:</p>
<pre><code>sage: a, b, c = K.basis()[0]
sage: a, b, c
(1, -34, 17)
</code></pre>
<p>Check if the linear relation in fact holds for the functions:</p>
<pre><code>sage: bool(a*f(x) + b*g(x) + c*h(x) == 0)
True
</code></pre>
https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56398#post-id-56398You 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.Fri, 26 Mar 2021 23:06:12 +0100https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56398#post-id-56398Comment by Presto.creator for <p>First an answer for the special case, then a more general answer.</p>
<h2>Trigonometric functions</h2>
<p>In the special case of the example in the question, this helps:</p>
<pre><code>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
</code></pre>
<h2>Linear independence of functions</h2>
<p>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.</p>
<p>Form a vectors from the value of each function at
a chosen collection of evaluation points.</p>
<p>If these vectors are linearly independent, then the functions are.</p>
<p>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.</p>
<p>Let us illustrate with the three functions above.</p>
<p>Choose a collection of evaluation points:</p>
<pre><code>sage: xx = (0, pi/6, pi/4, pi/3, pi/2)
</code></pre>
<p>Form a vector for each function:</p>
<pre><code>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])
</code></pre>
<p>To look for dependence relations, compute the left kernel
of the matrix with these vectors as rows.</p>
<pre><code>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]
</code></pre>
<p>This suggests that maybe <code>f - 34*g + 17*h</code> is zero.</p>
<p>Extract the coefficients:</p>
<pre><code>sage: a, b, c = K.basis()[0]
sage: a, b, c
(1, -34, 17)
</code></pre>
<p>Check if the linear relation in fact holds for the functions:</p>
<pre><code>sage: bool(a*f(x) + b*g(x) + c*h(x) == 0)
True
</code></pre>
https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56397#post-id-56397Hi 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?Fri, 26 Mar 2021 21:48:11 +0100https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56397#post-id-56397Comment by Presto.creator for <p>First an answer for the special case, then a more general answer.</p>
<h2>Trigonometric functions</h2>
<p>In the special case of the example in the question, this helps:</p>
<pre><code>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
</code></pre>
<h2>Linear independence of functions</h2>
<p>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.</p>
<p>Form a vectors from the value of each function at
a chosen collection of evaluation points.</p>
<p>If these vectors are linearly independent, then the functions are.</p>
<p>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.</p>
<p>Let us illustrate with the three functions above.</p>
<p>Choose a collection of evaluation points:</p>
<pre><code>sage: xx = (0, pi/6, pi/4, pi/3, pi/2)
</code></pre>
<p>Form a vector for each function:</p>
<pre><code>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])
</code></pre>
<p>To look for dependence relations, compute the left kernel
of the matrix with these vectors as rows.</p>
<pre><code>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]
</code></pre>
<p>This suggests that maybe <code>f - 34*g + 17*h</code> is zero.</p>
<p>Extract the coefficients:</p>
<pre><code>sage: a, b, c = K.basis()[0]
sage: a, b, c
(1, -34, 17)
</code></pre>
<p>Check if the linear relation in fact holds for the functions:</p>
<pre><code>sage: bool(a*f(x) + b*g(x) + c*h(x) == 0)
True
</code></pre>
https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56394#post-id-56394Thank you so much!Fri, 26 Mar 2021 21:35:47 +0100https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?comment=56394#post-id-56394Answer by Juanjo for <p>Is there a way with sage to show if two given functions are linearly dependent?
Like if I have </p>
<pre><code>f(x) = 17, g(x) = cos^2(x), h(x) = cos(2x)
</code></pre>
<p>Then sage could either give me the constants that would show they are linearly dependent, or say they aren't</p>
https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?answer=56404#post-id-56404Let $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](https://en.wikipedia.org/wiki/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.Sat, 27 Mar 2021 01:04:58 +0100https://ask.sagemath.org/question/56390/how-to-show-that-two-given-functions-are-linearly-dependent/?answer=56404#post-id-56404