def __init__(self): # An array of accessible functions self.functions = { # Logarithms 'ln': ln, 'log': lambda a, b=10: dmath.log(a, b), # Trigonometric Functions 'sin': lambda x: handle_type(sin(x)), 'cos': lambda x: handle_type(cos(x)), 'tan': lambda x: handle_type(tan(x)), 'arcsin': lambda x: handle_type(dmath.asin(x)), 'arccos': lambda x: handle_type(dmath.acos(x)), 'arctan': lambda x: handle_type(dmath.atan(x)), 'sinh': lambda x: handle_type(dmath.sinh(x)), 'cosh': lambda x: handle_type(dmath.cosh(x)), 'tanh': lambda x: handle_type(dmath.tanh(x)), 'arcsinh': lambda z: handle_type(log(z + (Integer(1) + z**Integer(2))**Real('0.5'))), 'arccosh': lambda z: ht(log(z + (z + Integer(1))**Real('0.5') * (z - Integer(1))**Real('0.5'))), 'arctanh': lambda z: handle_type(log(Integer(1) + z) - log(Integer(1) - z))/Integer(2), 'degrees': lambda x: handle_type(degrees(x)), # Statistics 'nCr': nCr, 'nPr': nPr, 'binomialpdf': binomialpdf, 'binomialcdf': binomialcdf, 'poissonpdf': poissonpdf, 'poissoncdf': poissoncdf, 'normalcdf': normalcdf, 'mean': lambda a: a.mean(), 'median': lambda a: a.median(), 'mode': lambda a: a.mode(), 'variance': lambda a: a.variance(), 'stdev': lambda a: a.stdev(), 'sxx': lambda a: a.Sxx(), # Manipulation of functions 'expand': expand, 'differentiate': lambda a, b=Symbol('x'):\ partial_differential(a, b), 'integrate': lambda y, a=None, b=None, x=Symbol('x'):\ partial_integral(y, x) if a == None or b == None \ else partial_integral(y, x).limit(a, b, variable=x), 'romberg': lambda f, a, b, *n: f.romberg_integral(a, b, *n), 'trapeziumrule': lambda f, a, b, *n:\ f.trapezoidal_integral(a, b, *n), 'simpsonrule': lambda f, a, b, *n: f.simpson_integral(a, b, *n), 'simpsonthreeeightrule': lambda f, a, b, *n: f.simpson38_integral(a, b, *n), 'roots': lambda a, n=1000: List(*list(a.roots(n))), 'maxima': lambda a, n=100: List(*a.maxima(n)), 'minima': lambda a, n=100: List(*a.minima(n)), # Vectors 'norm': lambda a: a.norm(), # Matrices 'transpose': lambda a: a.transpose(), 'order': lambda a: '{}×{}'.format(*a.order()), 'eval': lambda a, b, c=None: a(b, variable=c), 'identity': identity_matrix, 'diag': diagonal_matrix, 'inv': lambda a: a.inverse(), 'invert': lambda a: a.inverse(), 'decompose': lambda a: List(*a.LU_decomposition()), 'trace': lambda a: a.trace(), 'poly': lambda a: a.characteristic_polynomial(), 'adj': lambda a: a.adjgate(), 'zero': Matrix, 'minor': lambda a, b, c: a.minor(b, c), 'det': lambda a: a.determinant(), 'eigenvalues': lambda a: List(*a.eigenvalues()), 'rank': lambda a: a.rank(), # Complex Numbers 're': lambda a: a.real, 'im': lambda a: a.imag, 'arg': lambda a: a.argument(), 'conj': lambda a: a.conjugate(), # Misc 'yum': pi, 'plot': lambda f, a=-10, b=10: StrWithHtml('testing...', '''<canvas id="{0}" onclick="new CartesianPlot('{0}').simplePlot('{1}',{2},{3})" width="600" height="600">{4}</canvas>'''.format('graph-' + str(random.randint(1, 1000)), f, a, b, gnuplot(f, a, b).html)), 'polarplot': lambda f, a=-pi(), b=pi(): StrWithHtml('testing...', '''<canvas id="{0}" onclick="new PolarPlot('{0}').simplePlot('{1}',{2},{3})" width="600" height="600"></canvas>'''.format('graph-' + str(random.randint(1, 1000)), f, a, b)), 'testcanvas': lambda: StrWithHtml('testing...', '''<canvas id="testcanvas" width="600" height="600"></canvas>'''), 'evalbetween': evalute_between, 'factorial': factorial, 'factors': lambda a: a.factors(), 'decimal': lambda a: Decimal(a) if not isinstance(a, List)\ else List(*list(map(Decimal, a))), 'complex': lambda a: complex(a) if not isinstance(a, List)\ else List(*list(map(complex, a))), 'round': lambda a: handle_type(round), 'list': lambda a: str(list(a)), 'gnuplot': gnuplot, 'type': lambda a: str(type(a)), 'typelist': lambda b: ', '.join(map(lambda a: str(type(a)), b)), 'typematrix': lambda c: '; '.join(map(lambda b: ', '.join(map(lambda a: str(type(a)), b)), c)), 'setprec': self.set_precision, 'setexact': self.set_exact, 'about': lambda:\ StrWithHtml('Copyright Tom Wright <*****@*****.**>', '''<img src="./images/about.png" /> <br>This program was written by Tom Wright <*****@*****.**>'''), 'help': help.help, 'quit': exit, } # An array of accessible post functions self.post_functions = { '!': factorial, 'degs': radians } # An array of standard constants self.consts = { 'pi': pi(), 'g': Real('9.81'), 'h': Real('6.62606896e-34'), } # An array of miscellaneous internal variables such as ans # which stores the previous result self.objects = {'ans': Integer(0)}
Circle = namedtuple("Circle", "x y r") def circle_cross((x0, y0, r0), (x1, y1, r1)): d = vdist(Vec(x0, y0), Vec(x1, y1)) if d >= r0 + r1 or d <= abs(r0 - r1): return [] s = (r0 + r1 + d) / D2 a = sqrt(s * (s - d) * (s - r0) * (s - r1)) h = D2 * a / d dr = Vec(x1 - x0, y1 - y0) dx = vscale(sqrt(r0 ** 2 - h ** 2), vnorm(dr)) ang = vangle(dr) if \ r0 ** 2 + d ** 2 > r1 ** 2 \ else pi + vangle(dr) da = asin(h / r0) return map(anorm, [ang - da, ang + da]) # Angles of the start and end points of the circle arc. Angle2 = namedtuple("Angle2", "a1 a2") Arc = namedtuple("Arc", "c aa") arcPoint = lambda (x, y, r), a: \ vadd(Vec(x, y), Vec(r * cos(a), r * sin(a))) arc_start = lambda (c, (a0, a1)): arcPoint(c, a0) arc_mid = lambda (c, (a0, a1)): arcPoint(c, (a0 + a1) / D2) arc_end = lambda (c, (a0, a1)): arcPoint(c, a1) arc_center = lambda ((x, y, r), _): Vec(x, y)
def test_asin(): for x in range(-1, 1): assert dmath.asin(x) == math.asin(x) assert grad(dmath.asin)(x) == approx(1 / math.sqrt(1 - x**2))