@@ -129,6 +129,7 @@ def __call__(self, function_pieces, **kwds):
129
129
(-1, 0, 1)
130
130
"""
131
131
#print 'pf_call', function_pieces, kwds
132
+ from types import *
132
133
var = kwds .pop ('var' , None )
133
134
parameters = []
134
135
domain_list = []
@@ -138,6 +139,13 @@ def __call__(self, function_pieces, **kwds):
138
139
domain = RealSet (domain )
139
140
if domain .is_empty ():
140
141
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 )
141
149
function = SR (function )
142
150
if var is None and len (function .variables ()) > 0 :
143
151
var = function .variables ()[0 ]
@@ -967,5 +975,166 @@ def func(x0, x1):
967
975
rsum += func (x0 , x1 )
968
976
return piecewise (rsum )
969
977
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 ()
970
1139
971
1140
piecewise = PiecewiseFunction ()
0 commit comments