Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit b89ee3e

Browse files
committed
14801: last part of additions and fixes
1 parent e0b44e4 commit b89ee3e

File tree

3 files changed

+171
-2
lines changed

3 files changed

+171
-2
lines changed

src/doc/en/constructions/plotting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ You can plot piecewise-defined functions:
4040
sage: f4 = sin(2*x)
4141
sage: f = piecewise([((0,1),f1), ((1,2),f2), ((2,3),f3), ((3,10),f4)])
4242
sage: f.plot(x,0,10)
43-
Graphics object consisting of 4 graphics primitives
43+
Graphics object consisting of 1 graphics primitives
4444

4545
Other function plots can be produced as well:
4646

src/sage/functions/piecewise.py

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def __call__(self, function_pieces, **kwds):
129129
(-1, 0, 1)
130130
"""
131131
#print 'pf_call', function_pieces, kwds
132+
from types import *
132133
var = kwds.pop('var', None)
133134
parameters = []
134135
domain_list = []
@@ -138,6 +139,13 @@ def __call__(self, function_pieces, **kwds):
138139
domain = RealSet(domain)
139140
if domain.is_empty():
140141
continue
142+
if isinstance(function, FunctionType):
143+
if var is None:
144+
var = SR.var('x')
145+
if function.func_code.co_argcount == 0:
146+
function = function()
147+
else:
148+
function = function(var)
141149
function = SR(function)
142150
if var is None and len(function.variables()) > 0:
143151
var = function.variables()[0]
@@ -967,5 +975,166 @@ def func(x0, x1):
967975
rsum += func(x0, x1)
968976
return piecewise(rsum)
969977

978+
def laplace(cls, self, parameters, variable, x='x', s='t'):
979+
r"""
980+
Returns the Laplace transform of self with respect to the variable
981+
var.
982+
983+
INPUT:
984+
985+
- ``x`` - variable of self
986+
987+
- ``s`` - variable of Laplace transform.
988+
989+
We assume that a piecewise function is 0 outside of its domain and
990+
that the left-most endpoint of the domain is 0.
991+
992+
EXAMPLES::
993+
994+
sage: x, s, w = var('x, s, w')
995+
sage: f = piecewise([[(0,1),1],[[1,2], 1-x]])
996+
sage: f.laplace(x, s)
997+
-e^(-s)/s + (s + 1)*e^(-2*s)/s^2 + 1/s - e^(-s)/s^2
998+
sage: f.laplace(x, w)
999+
-e^(-w)/w + (w + 1)*e^(-2*w)/w^2 + 1/w - e^(-w)/w^2
1000+
1001+
::
1002+
1003+
sage: y, t = var('y, t')
1004+
sage: f = piecewise([[[1,2], 1-y]])
1005+
sage: f.laplace(y, t)
1006+
(t + 1)*e^(-2*t)/t^2 - e^(-t)/t^2
1007+
1008+
::
1009+
1010+
sage: s = var('s')
1011+
sage: t = var('t')
1012+
sage: f1(t) = -t
1013+
sage: f2(t) = 2
1014+
sage: f = piecewise([[[0,1],f1],[(1,infinity),f2]])
1015+
sage: f.laplace(t,s)
1016+
(s + 1)*e^(-s)/s^2 + 2*e^(-s)/s - 1/s^2
1017+
"""
1018+
from sage.all import assume, exp, forget
1019+
x = SR.var(x)
1020+
s = SR.var(s)
1021+
assume(s>0)
1022+
result = 0
1023+
for domain, f in parameters:
1024+
for interval in domain:
1025+
a = interval.lower()
1026+
b = interval.upper()
1027+
result += (SR(f)*exp(-s*x)).integral(x,a,b)
1028+
forget(s>0)
1029+
return result
1030+
1031+
def fourier_series_cosine_coefficient(cls, self, parameters, variable, n, L):
1032+
r"""
1033+
Returns the n-th Fourier series coefficient of
1034+
`\cos(n\pi x/L)`, `a_n`.
1035+
1036+
INPUT:
1037+
1038+
1039+
- ``self`` - the function f(x), defined over -L x L
1040+
1041+
- ``n`` - an integer n=0
1042+
1043+
- ``L`` - (the period)/2
1044+
1045+
1046+
OUTPUT:
1047+
`a_n = \frac{1}{L}\int_{-L}^L f(x)\cos(n\pi x/L)dx`
1048+
1049+
EXAMPLES::
1050+
1051+
sage: f(x) = x^2
1052+
sage: f = piecewise([[(-1,1),f]])
1053+
sage: f.fourier_series_cosine_coefficient(2,1)
1054+
pi^(-2)
1055+
sage: f(x) = x^2
1056+
sage: f = piecewise([[(-pi,pi),f]])
1057+
sage: f.fourier_series_cosine_coefficient(2,pi)
1058+
1
1059+
sage: f1(x) = -1
1060+
sage: f2(x) = 2
1061+
sage: f = piecewise([[(-pi,pi/2),f1],[(pi/2,pi),f2]])
1062+
sage: f.fourier_series_cosine_coefficient(5,pi)
1063+
-3/5/pi
1064+
"""
1065+
from sage.all import cos, pi
1066+
x = SR.var('x')
1067+
result = 0
1068+
for domain, f in parameters:
1069+
for interval in domain:
1070+
a = interval.lower()
1071+
b = interval.upper()
1072+
result += (f*cos(pi*x*n/L)/L).integrate(x, a, b)
1073+
return SR(result).simplify_trig()
1074+
1075+
def fourier_series_sine_coefficient(cls, self, parameters, variable, n, L):
1076+
r"""
1077+
Returns the n-th Fourier series coefficient of
1078+
`\sin(n\pi x/L)`, `b_n`.
1079+
1080+
INPUT:
1081+
1082+
1083+
- ``self`` - the function f(x), defined over -L x L
1084+
1085+
- ``n`` - an integer n0
1086+
1087+
- ``L`` - (the period)/2
1088+
1089+
1090+
OUTPUT:
1091+
`b_n = \frac{1}{L}\int_{-L}^L f(x)\sin(n\pi x/L)dx`
1092+
1093+
EXAMPLES::
1094+
1095+
sage: f(x) = x^2
1096+
sage: f = piecewise([[(-1,1),f]])
1097+
sage: f.fourier_series_sine_coefficient(2,1) # L=1, n=2
1098+
0
1099+
"""
1100+
from sage.all import sin, pi
1101+
x = SR.var('x')
1102+
result = 0
1103+
for domain, f in parameters:
1104+
for interval in domain:
1105+
a = interval.lower()
1106+
b = interval.upper()
1107+
result += (f*sin(pi*x*n/L)/L).integrate(x, a, b)
1108+
return SR(result).simplify_trig()
1109+
1110+
def fourier_series_partial_sum(cls, self, parameters, variable, N, L):
1111+
r"""
1112+
Returns the partial sum
1113+
1114+
.. math::
1115+
1116+
f(x) \sim \frac{a_0}{2} + \sum_{n=1}^N [a_n\cos(\frac{n\pi x}{L}) + b_n\sin(\frac{n\pi x}{L})],
1117+
1118+
as a string.
1119+
1120+
EXAMPLE::
1121+
1122+
sage: f(x) = x^2
1123+
sage: f = piecewise([[(-1,1),f]])
1124+
sage: f.fourier_series_partial_sum(3,1)
1125+
cos(2*pi*x)/pi^2 - 4*cos(pi*x)/pi^2 + 1/3
1126+
sage: f1(x) = -1
1127+
sage: f2(x) = 2
1128+
sage: f = piecewise([[(-pi,pi/2),f1],[(pi/2,pi),f2]])
1129+
sage: f.fourier_series_partial_sum(3,pi)
1130+
-3*cos(x)/pi - 3*sin(2*x)/pi + 3*sin(x)/pi - 1/4
1131+
"""
1132+
from sage.all import pi, sin, cos, srange
1133+
x = self.default_variable()
1134+
a0 = self.fourier_series_cosine_coefficient(0,L)
1135+
result = a0/2 + sum([(self.fourier_series_cosine_coefficient(n,L)*cos(n*pi*x/L) +
1136+
self.fourier_series_sine_coefficient(n,L)*sin(n*pi*x/L))
1137+
for n in srange(1,N)])
1138+
return SR(result).expand()
9701139

9711140
piecewise = PiecewiseFunction()

src/sage/functions/piecewise_old.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def trapezoid(self,N):
538538
sage: Q = tf.plot(rgbcolor=(0.7,0.6,0.6), plot_points=40)
539539
sage: L = add([line([[a,0],[a,f(a)]],rgbcolor=(0.7,0.6,0.6)) for (a,b),f in tf.list()])
540540
sage: P+Q+L
541-
Graphics object consisting of 14 graphics primitives
541+
Graphics object consisting of 13 graphics primitives
542542
543543
TESTS:
544544

0 commit comments

Comments
 (0)