Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def expr_period(expr):
if expr.has(sympy.cos):
function_type = sympy.cos
elif expr.has(sympy.sin):
function_type = sympy.sin
elif expr.has(sympy.tan):
function_type = sympy.tan
elif expr.has(sympy.cot):
function_type = sympy.cot
interior = expr.find(function_type).pop().args[0]
if function_type in [sympy.sin, sympy.cos]:
return abs(2 * sympy.pi / interior.coeff(x))
elif function_type in [sympy.tan, sympy.cot]:
return abs(sympy.pi / interior.coeff(x))
import sympy as sm
x, L, C, D, c_0, c_1, = sm.symbols('x L C D c_0 c_1')
f = sm.sin(x)
def model1(f, L, D):
"""Solve -u'' = f(x), u(0)=0, u(L)=D."""
u_x = - sm.integrate(f, (x, 0, x)) + c_0
u = sm.integrate(u_x, (x, 0, x)) + c_1
r = sm.solve([u.subs(x, 0)-0, u.subs(x,L)-D], [c_0, c_1])
u = u.subs(c_0, r[c_0]).subs(c_1, r[c_1])
u = sm.simplify(sm.expand(u))
return u
def model2(f, L, C, D):
"""Solve -u'' = f(x), u'(0)=C, u(L)=D."""
u_x = - sm.integrate(f, (x, 0, x)) + c_0
u = sm.integrate(u_x, (x, 0, x)) + c_1
r = sm.solve([sm.diff(u,x).subs(x, 0)-C, u.subs(x,L)-D], [c_0, c_1])
u = u.subs(c_0, r[c_0]).subs(c_1, r[c_1])
try:
critical_angle_ = critical_angle(medium1, medium2)
except (ValueError, TypeError):
critical_angle_ = None
if angle_of_incidence is not None:
if normal is not None or plane is not None:
raise ValueError('Normal/plane not allowed if incident is an angle')
if not 0.0 <= angle_of_incidence < pi*0.5:
raise ValueError('Angle of incidence not in range [0:pi/2)')
if critical_angle_ and angle_of_incidence > critical_angle_:
raise ValueError('Ray undergoes total internal reflection')
return asin(n1*sin(angle_of_incidence)/n2)
if angle_of_incidence and not 0 <= angle_of_incidence < pi*0.5:
raise ValueError
# Treat the incident as ray below
# A flag to check whether to return Ray3D or not
return_ray = False
if plane is not None and normal is not None:
raise ValueError("Either plane or normal is acceptable.")
if not isinstance(incident, Matrix):
if is_sequence(incident):
_incident = Matrix(incident)
elif isinstance(incident, Ray3D):
_incident = Matrix(incident.direction_ratio)
def main(n):
'''Solves grad-div problem in 2d with HypreAMS preconditioning'''
# Exact solution
x, y = sp.symbols('x[0] x[1]')
sigma = sp.Matrix([sp.cos(pi*y**2), sp.sin(pi*x**2)])
sp_div = lambda f: f[0].diff(x, 1) + f[1].diff(y, 1)
sp_grad = lambda f: sp.Matrix([f.diff(x, 1), f.diff(y, 1)])
f = -sp_grad(sp_div(sigma)) + sigma
sigma_expr, f_expr = map(as_expression, (sigma, f))
# The discrete problem
mesh = UnitSquareMesh(n, n)
V = FunctionSpace(mesh, 'RT', 1)
u, v = TrialFunction(V), TestFunction(V)
a = inner(div(u), div(v))*dx + inner(u, v)*dx
def lambda_s0(c1, c2):
return c1*(q - p)*abs(omega)**(1/(q - p))*sin(psi) \
+ c2*(v - u)*abs(sigma)**(1/(v - u))*sin(theta)
lambda_s = Piecewise(
# Program 02f: A linear first order ODE.
from sympy import Function, dsolve, Eq, symbols, sin
t = symbols('t');
I = symbols('I', cls=Function)
sol = dsolve(Eq(I(t).diff(t), 5*sin(t) - I(t)/5), I(t))
print(sol)
x = xt[0]
phi = xt[2]
# next we set some parameters
car_width = 0.05
car_heigth = 0.02
rod_length = 0.5
pendulum_size = 0.015
# then we determine the current state of the system
# according to the given simulation data
x_car = x
y_car = 0
x_pendulum = -rod_length * sin(phi) + x_car
y_pendulum = rod_length * cos(phi)
# now we can build the image
# the pendulum will be represented by a black circle with
# center: (x_pendulum, y_pendulum) and radius `pendulum_size
pendulum = mpl.patches.Circle(xy=(x_pendulum, y_pendulum), radius=pendulum_size, color='black')
# the cart will be represented by a grey rectangle with
# lower left: (x_car - 0.5 * car_width, y_car - car_heigth)
# width: car_width
# height: car_height
car = mpl.patches.Rectangle((x_car-0.5*car_width, y_car-car_heigth), car_width, car_heigth,
fill=True, facecolor='grey', linewidth=2.0)
# the joint will also be a black circle with
def g3(field, phi, theta):
return (sin(theta) * cos(phi) * field.dx(x0=x+x.spacing/2) +
sin(theta) * sin(phi) * field.dy(x0=y+y.spacing/2) +
cos(theta) * field.dz(x0=z+z.spacing/2))
import logging
import rbf.domain
import scipy.sparse
import sympy
logging.basicConfig(level=logging.DEBUG)
# set default cmap to viridis if you have it
if 'viridis' in vars(cm):
plt.rcParams['image.cmap'] = 'viridis'
# total number of nodes
N = 200
# symbolic definition of the solution
x,y = sympy.symbols('x,y')
r = sympy.sqrt(x**2 + y**2)
true_soln_sym = (1-r)*sympy.sin(x)*sympy.cos(y)
# numerical solution
true_soln = sympy.lambdify((x,y),true_soln_sym,'numpy')
# symbolic forcing term
forcing_sym = true_soln_sym.diff(x,x) + true_soln_sym.diff(y,y)
# numerical forcing term
forcing = sympy.lambdify((x,y),forcing_sym,'numpy')
# define a circular domain
vert,smp = rbf.domain.circle()
nodes,smpid = menodes(N,vert,smp)
# smpid describes which boundary simplex, if any, the nodes are
# attached to. If it is -1, then the node is in the interior
boundary, = (smpid>=0).nonzero()
interior, = (smpid==-1).nonzero()
def draw(xti, image):
phi1, phi2 = xti[0], xti[2]
L =0.4
x1 = L*cos(phi1)
y1 = L*sin(phi1)
x2 = x1+L*cos(phi2+phi1)
y2 = y1+L*sin(phi2+phi1)
# rods
rod1 = mpl.lines.Line2D([0,x1],[0,y1],color='k',zorder=0,linewidth=2.0)
rod2 = mpl.lines.Line2D([x1,x2],[y1,y2],color='k',zorder=0,linewidth=2.0)
# pendulums
sphere1 = mpl.patches.Circle((x1,y1),0.01,color='k')
sphere2 = mpl.patches.Circle((0,0),0.01,color='k')
image.lines.append(rod1)
image.lines.append(rod2)
image.patches.append(sphere1)
image.patches.append(sphere2)
return image