def evaluateTotalError(f, g, h, a, b, n): ''' args: f - function being interpolated g - equally spaced polynomial h - Chebyshev polynomial a - left endpoint of integral b - right endpoint of integral n - number of returns: total error Numerically integrates |f(x)-g(x)| from a to b This is exactly the "difference" between the original function and the interpolation polynomial that is currently being analyzed. ''' g_error = definite_integral(f - g, x, a, b) h_error = definite_integral(f - h, x, a, b) print("|f(x)-g(x)| from " + a + " to " + b + ": " + g_error) print("|f(x)-h(x)| from " + a + " to " + b + ": " + h_error) return total_error
def I4_GST(C_in, b): """ This function calculates the 4th invariant (a.k.a., GST invariant) # Input: # C_in -- Right Cauchy-Green deformation tensor # b -- controlling parameter for fiber dispersion # The mean fiber direciton is assumed to be aligned along the x direction # The fiber distribution rho=e^(b*(cos(2theta)+1)) """ from sage.symbolic.integration.integral import definite_integral var('theta, phi') wlambda=(lambda_f(C_in, theta=theta, phi=phi) *rhom(theta, phi, b=b)).simplify_full() e_int_phi=definite_integral(wlambda, phi, 0, 2*pi) # KKK here actually equals to 4*pi*K kkk=numerical_integral(rhom(theta, phi=0, b=b) *sin(theta), 0, pi)[0]*2*pi ops=e_int_phi.expand().operands() E_i=0 for iop in ops: # separate each term into a part that can be numerically integrated and a part that can't be numerically integrated (fun_wvar, fun_wovar)=separate(iop, theta) tobeitgd=sin(theta)*fun_wvar itgd=numerical_integral(tobeitgd, 0, pi)[0]/kkk * fun_wovar E_i+=itgd return (E_i-1.)
def I4_CH(C_in, b): r""" This function calculates the characteristic invariant Lazy bone version -- using numerical integral :) """ from sage.symbolic.integration.integral import definite_integral var('theta, phi') wlambda=((lambda_f(C_in, theta=theta, phi=phi)-1)**2 *rhom(theta, phi, b=b)) e_int_phi=definite_integral(wlambda, phi, 0, 2*pi) # KKK here actually equals to 4*pi*K kkk=numerical_integral(rhom(theta, phi=0, b=b) *sin(theta), 0, pi)[0]*2*pi ops=e_int_phi.expand().operands() Lambda_i=0 for iop in ops: # separate each term into a part that can be numerically integrated and a part that can't be numerically integrated (fun_wvar, fun_wovar)=separate(iop, theta) tobeitgd=sin(theta)*fun_wvar itgd=numerical_integral(tobeitgd, 0, pi)[0]/kkk * fun_wovar Lambda_i+=itgd return Lambda_i
def thomson(): var("theta,phi,beta,S0,S1,S2,S3,a", domain="real") S = vector([S0, S1, S2, S3]) M1 = MuellerMatrixRotation(beta) M2 = jones_to_mueller(JonesMatrixThomson(theta)) # print mueller_to_jones(M2) M2 = M2.substitute(theta=acos(sqrt(_sage_const_2 * a - _sage_const_1))).simplify() M = (M2 * M1).simplify() print "\nMueller matrix:" print M S_scat = (M * S).simplify() I_scat = S_scat[_sage_const_0].substitute(beta=pi / _sage_const_2 - phi).simplify() print "\nScattered intensity:" print I_scat It_scat = I_scat.substitute(a=(_sage_const_1 + cos(theta)**_sage_const_2) / _sage_const_2) print "\nScattered intensity (unpol):" print It_scat.substitute(S1=_sage_const_0).substitute( S2=_sage_const_0).simplify_full() print "\nScattered intensity (horizontal linear pol.):" print It_scat.substitute(S1=S0).substitute( S2=_sage_const_0).simplify_full() It_scat = definite_integral(It_scat * sin(theta), theta, _sage_const_0, pi) It_scat = definite_integral(It_scat, phi, _sage_const_0, _sage_const_2 * pi) print "\nThomson cross-section (units of r_e^2):" print It_scat / S0
def convolution(cls, self, parameters, variable, other): """ Return the convolution function, `f*g(t)=\int_{-\infty}^\infty f(u)g(t-u)du`, for compactly supported `f,g`. EXAMPLES:: sage: x = PolynomialRing(QQ,'x').gen() sage: f = piecewise([[[0,1],1]]) ## example 0 sage: g = f.convolution(f); g piecewise(x|-->x on (0, 1], x|-->-x + 2 on (1, 2]; x) sage: h = f.convolution(g); h piecewise(x|-->1/2*x^2 on (0, 1], x|-->-x^2 + 3*x - 3/2 on (1, 2], x|-->1/2*x^2 - 3*x + 9/2 on (2, 3]; x) sage: f = piecewise([[(0,1),1],[(1,2),2],[(2,3),1]]) ## example 1 sage: g = f.convolution(f) sage: h = f.convolution(g); h piecewise(x|-->1/2*x^2 on (0, 1], x|-->2*x^2 - 3*x + 3/2 on (1, 3], x|-->-2*x^2 + 21*x - 69/2 on (3, 4], x|-->-5*x^2 + 45*x - 165/2 on (4, 5], x|-->-2*x^2 + 15*x - 15/2 on (5, 6], x|-->2*x^2 - 33*x + 273/2 on (6, 8], x|-->1/2*x^2 - 9*x + 81/2 on (8, 9]; x) sage: f = piecewise([[(-1,1),1]]) ## example 2 sage: g = piecewise([[(0,3),x]]) sage: f.convolution(g) piecewise(x|-->1/2*x^2 + x + 1/2 on (-1, 1], x|-->2*x on (1, 2], x|-->-1/2*x^2 + x + 4 on (2, 4]; x) sage: g = piecewise([[(0,3),1],[(3,4),2]]) sage: f.convolution(g) piecewise(x|-->x + 1 on (-1, 1], x|-->2 on (1, 2], x|-->x on (2, 3], x|-->-x + 6 on (3, 4], x|-->-2*x + 10 on (4, 5]; x) Check that the bugs raised in :trac:`12123` are fixed:: sage: f = piecewise([[(-2, 2), 2]]) sage: g = piecewise([[(0, 2), 3/4]]) sage: f.convolution(g) piecewise(x|-->3/2*x + 3 on (-2, 0], x|-->3 on (0, 2], x|-->-3/2*x + 6 on (2, 4]; x) sage: f = piecewise([[(-1, 1), 1]]) sage: g = piecewise([[(0, 1), x], [(1, 2), -x + 2]]) sage: f.convolution(g) piecewise(x|-->1/2*x^2 + x + 1/2 on (-1, 0], x|-->-1/2*x^2 + x + 1/2 on (0, 2], x|-->1/2*x^2 - 3*x + 9/2 on (2, 3]; x) """ from sage.symbolic.integration.integral import definite_integral f = self g = other if len(f.end_points())*len(g.end_points()) == 0: raise ValueError('one of the piecewise functions is nowhere defined') M = min(min(f.end_points()),min(g.end_points())) N = max(max(f.end_points()),max(g.end_points())) tt = SR.var('tt') uu = SR.var('uu') conv = 0 fd,f0 = parameters[0] gd,g0 = next(other.items()) if len(f)==1 and len(g)==1: f = f.unextend_zero() g = g.unextend_zero() a1 = fd[0].lower() a2 = fd[0].upper() b1 = gd[0].lower() b2 = gd[0].upper() i1 = f0.subs({variable: uu}) i2 = g0.subs({variable: tt-uu}) fg1 = definite_integral(i1*i2, uu, a1, tt-b1).subs(tt = variable) fg2 = definite_integral(i1*i2, uu, tt-b2, tt-b1).subs(tt = variable) fg3 = definite_integral(i1*i2, uu, tt-b2, a2).subs(tt = variable) fg4 = definite_integral(i1*i2, uu, a1, a2).subs(tt = variable) if a1-b1<a2-b2: if a2+b1!=a1+b2: h = piecewise([[(a1+b1,a1+b2),fg1],[(a1+b2,a2+b1),fg2],[(a2+b1,a2+b2),fg3]]) else: h = piecewise([[(a1+b1,a1+b2),fg1],[(a1+b2,a2+b2),fg3]]) else: if a1+b2!=a2+b1: h = piecewise([[(a1+b1,a2+b1),fg1],[(a2+b1,a1+b2),fg4],[(a1+b2,a2+b2),fg3]]) else: h = piecewise([[(a1+b1,a2+b1),fg1],[(a2+b1,a2+b2),fg3]]) return (piecewise([[(minus_infinity,infinity),0]]).piecewise_add(h)).unextend_zero() if len(f)>1 or len(g)>1: z = piecewise([[(0,0),0]]) for fpiece in f.pieces(): for gpiece in g.pieces(): h = gpiece.convolution(fpiece) z = z.piecewise_add(h) return z.unextend_zero()
def compton(): var("theta,phi,beta,S0,S1,S2,S3,a,c,costheta,s,E,Esc", domain="real") assume(E > _sage_const_0) assume(Esc > _sage_const_0) S = vector([S0, S1, S2, S3]) M1 = MuellerMatrixRotation(beta) M2 = (matrix( SR, _sage_const_4, _sage_const_4, [ a + c, _sage_const_1 - a, _sage_const_0, _sage_const_0, _sage_const_1 - a, a, _sage_const_0, _sage_const_0, _sage_const_0, _sage_const_0, cos(theta), _sage_const_0, _sage_const_0, _sage_const_0, _sage_const_0, cos(theta) * (_sage_const_1 + c), ], ) * s) # print mueller_to_jones(M2) M2 = M2.substitute(theta=acos(sqrt(_sage_const_2 * a - _sage_const_1))).simplify() M = (M2 * M1).simplify() print "\nMueller matrix:" print M S_scat = (M * S).simplify() I_scat = S_scat[_sage_const_0].substitute(beta=pi / _sage_const_2 - phi).simplify() print "\nScattered intensity:" print I_scat # Energy in units of m_e*c^2 It_scat = I_scat.substitute(a=(_sage_const_1 + cos(theta)**_sage_const_2) / _sage_const_2) It_scat = It_scat.substitute(s=Esc**_sage_const_2 / E**_sage_const_2) It_scat = It_scat.substitute(c=(E - Esc) / _sage_const_2 * (_sage_const_1 - cos(theta))) It_scat = It_scat.simplify_full() print "\nScattered intensity (unpol):" print It_scat.substitute(S1=_sage_const_0).substitute( S2=_sage_const_0).simplify_full() It_scatu1 = It_scat.substitute(S1=_sage_const_0).substitute( S2=_sage_const_0) It_scatu2 = (Esc**_sage_const_2 / E**_sage_const_2 * S0 * (E / Esc + Esc / E - sin(theta)**_sage_const_2) / _sage_const_2) It_scatu1 = (It_scatu1.substitute( Esc=E / (_sage_const_1 + E * (_sage_const_1 - cos(theta)))).simplify_full().simplify_trig()) It_scatu2 = (It_scatu2.substitute( Esc=E / (_sage_const_1 + E * (_sage_const_1 - cos(theta)))).simplify_full().simplify_trig()) print bool(It_scatu1 == It_scatu2) It_scatu1 = It_scat.substitute(S1=S0).substitute(S2=_sage_const_0) It_scatu2 = ( Esc**_sage_const_2 / E**_sage_const_2 * S0 * (E / Esc + Esc / E - _sage_const_2 * sin(theta)**_sage_const_2 * cos(phi)**_sage_const_2) / _sage_const_2) It_scatu1 = (It_scatu1.substitute( Esc=E / (_sage_const_1 + E * (_sage_const_1 - cos(theta)))).simplify_full().simplify_trig()) It_scatu2 = (It_scatu2.substitute( Esc=E / (_sage_const_1 + E * (_sage_const_1 - cos(theta)))).simplify_full().simplify_trig()) print bool(It_scatu1 == It_scatu2) print "\nScattered intensity (horizontal linear pol.):" print It_scat.substitute(S1=S0).substitute( S2=_sage_const_0).simplify_full() It_scat = It_scat.substitute(Esc=E / (_sage_const_1 + E * (_sage_const_1 - cos(theta)))) It_scat = It_scat.simplify_full() It_scat = definite_integral(It_scat * sin(theta), theta, _sage_const_0, pi) It_scat = definite_integral(It_scat, phi, _sage_const_0, _sage_const_2 * pi) print "\nKlein-Nishina cross-section (units of r_e^2):" print(It_scat / S0).simplify_full()
def convolution(cls, self, parameters, variable, other): """ Return the convolution function, `f*g(t)=\int_{-\infty}^\infty f(u)g(t-u)du`, for compactly supported `f,g`. EXAMPLES:: sage: x = PolynomialRing(QQ,'x').gen() sage: f = piecewise([[[0,1],1]]) ## example 0 sage: g = f.convolution(f); g piecewise(x|-->x on (0, 1], x|-->-x + 2 on (1, 2]; x) sage: h = f.convolution(g); h piecewise(x|-->1/2*x^2 on (0, 1], x|-->-x^2 + 3*x - 3/2 on (1, 2], x|-->1/2*x^2 - 3*x + 9/2 on (2, 3]; x) sage: f = piecewise([[(0,1),1],[(1,2),2],[(2,3),1]]) ## example 1 sage: g = f.convolution(f) sage: h = f.convolution(g); h piecewise(x|-->1/2*x^2 on (0, 1], x|-->2*x^2 - 3*x + 3/2 on (1, 3], x|-->-2*x^2 + 21*x - 69/2 on (3, 4], x|-->-5*x^2 + 45*x - 165/2 on (4, 5], x|-->-2*x^2 + 15*x - 15/2 on (5, 6], x|-->2*x^2 - 33*x + 273/2 on (6, 8], x|-->1/2*x^2 - 9*x + 81/2 on (8, 9]; x) sage: f = piecewise([[(-1,1),1]]) ## example 2 sage: g = piecewise([[(0,3),x]]) sage: f.convolution(g) piecewise(x|-->1/2*x^2 + x + 1/2 on (-1, 1], x|-->2*x on (1, 2], x|-->-1/2*x^2 + x + 4 on (2, 4]; x) sage: g = piecewise([[(0,3),1],[(3,4),2]]) sage: f.convolution(g) piecewise(x|-->x + 1 on (-1, 1], x|-->2 on (1, 2], x|-->x on (2, 3], x|-->-x + 6 on (3, 4], x|-->-2*x + 10 on (4, 5]; x) Check that the bugs raised in :trac:`12123` are fixed:: sage: f = piecewise([[(-2, 2), 2]]) sage: g = piecewise([[(0, 2), 3/4]]) sage: f.convolution(g) piecewise(x|-->3/2*x + 3 on (-2, 0], x|-->3 on (0, 2], x|-->-3/2*x + 6 on (2, 4]; x) sage: f = piecewise([[(-1, 1), 1]]) sage: g = piecewise([[(0, 1), x], [(1, 2), -x + 2]]) sage: f.convolution(g) piecewise(x|-->1/2*x^2 + x + 1/2 on (-1, 0], x|-->-1/2*x^2 + x + 1/2 on (0, 2], x|-->1/2*x^2 - 3*x + 9/2 on (2, 3]; x) """ from sage.symbolic.integration.integral import definite_integral f = self g = other if len(f.end_points()) * len(g.end_points()) == 0: raise ValueError( 'one of the piecewise functions is nowhere defined') M = min(min(f.end_points()), min(g.end_points())) N = max(max(f.end_points()), max(g.end_points())) tt = SR.var('tt') uu = SR.var('uu') conv = 0 fd, f0 = parameters[0] gd, g0 = next(other.items()) if len(f) == 1 and len(g) == 1: f = f.unextend_zero() g = g.unextend_zero() a1 = fd[0].lower() a2 = fd[0].upper() b1 = gd[0].lower() b2 = gd[0].upper() i1 = f0.subs({variable: uu}) i2 = g0.subs({variable: tt - uu}) fg1 = definite_integral(i1 * i2, uu, a1, tt - b1).subs(tt=variable) fg2 = definite_integral(i1 * i2, uu, tt - b2, tt - b1).subs(tt=variable) fg3 = definite_integral(i1 * i2, uu, tt - b2, a2).subs(tt=variable) fg4 = definite_integral(i1 * i2, uu, a1, a2).subs(tt=variable) if a1 - b1 < a2 - b2: if a2 + b1 != a1 + b2: h = piecewise([[(a1 + b1, a1 + b2), fg1], [(a1 + b2, a2 + b1), fg2], [(a2 + b1, a2 + b2), fg3]]) else: h = piecewise([[(a1 + b1, a1 + b2), fg1], [(a1 + b2, a2 + b2), fg3]]) else: if a1 + b2 != a2 + b1: h = piecewise([[(a1 + b1, a2 + b1), fg1], [(a2 + b1, a1 + b2), fg4], [(a1 + b2, a2 + b2), fg3]]) else: h = piecewise([[(a1 + b1, a2 + b1), fg1], [(a2 + b1, a2 + b2), fg3]]) return (piecewise([[(minus_infinity, infinity), 0]]).piecewise_add(h)).unextend_zero() if len(f) > 1 or len(g) > 1: z = piecewise([[(0, 0), 0]]) for fpiece in f.pieces(): for gpiece in g.pieces(): h = gpiece.convolution(fpiece) z = z.piecewise_add(h) return z.unextend_zero()
def cdf_from_pdf(self, x): return definite_integral(self.pdf(self.tmpvar), self.tmpvar, self.a, x).simplify_full()
var("R") var("A") var("B") var("beta") var("delta") # Elastic scattering Kunpol = (_sage_const_1 + (cos(theta))**_sage_const_2) / _sage_const_2 Kpol = Kunpol - (sin(theta))**_sage_const_2 / _sage_const_2 * ( P * cos(_sage_const_2 * phi) + sqrt(_sage_const_1 - P**_sage_const_2) * cos(delta) * sin(_sage_const_2 * phi)) __tmp__ = var("theta,phi") ElasticDiffPol2 = symbolic_expression(Kpol).function(theta, phi) __tmp__ = var("theta") ElasticDiffPol1 = symbolic_expression( definite_integral(ElasticDiffPol2(theta, phi), phi, _sage_const_0, _sage_const_2 * pi)).function(theta) __tmp__ = var("theta,phi") ElasticDiffUnPol2 = symbolic_expression(Kunpol).function(theta, phi) __tmp__ = var("theta") ElasticDiffUnPol1 = symbolic_expression( definite_integral(ElasticDiffUnPol2(theta, phi), phi, _sage_const_0, _sage_const_2 * pi)).function(theta) assert ElasticDiffPol1(theta) == ElasticDiffUnPol1(theta) assert (definite_integral( ElasticDiffUnPol1(theta) * sin(theta), theta, _sage_const_0, pi) == _sage_const_8 * pi / _sage_const_3) # Inelastic scattering __tmp__ = var("theta,phi") InelasticDiffPol2 = symbolic_expression(