How do I compute derivative using Numpy?

You have four options Finite Differences Automatic Derivatives Symbolic Differentiation Compute derivatives by hand. Finite differences require no external tools but are prone to numerical error and, if you’re in a multivariate situation, can take a while. Symbolic differentiation is ideal if your problem is simple enough. Symbolic methods are getting quite robust these days. … Read more

How can I check if two segments intersect?

User @i_4_got points to this page with a very efficent solution in Python. I reproduce it here for convenience (since it would have made me happy to have it here): def ccw(A,B,C): return (C.y-A.y) * (B.x-A.x) > (B.y-A.y) * (C.x-A.x) # Return true if line segments AB and CD intersect def intersect(A,B,C,D): return ccw(A,C,D) != … Read more

Evaluating a mathematical expression in a string

eval is evil eval(“__import__(‘os’).remove(‘important file’)”) # arbitrary commands eval(“9**9**9**9**9**9**9**9”, {‘__builtins__’: None}) # CPU, memory Note: even if you use set __builtins__ to None it still might be possible to break out using introspection: eval(‘(1).__class__.__bases__[0].__subclasses__()’, {‘__builtins__’: None}) Evaluate arithmetic expression using ast import ast import operator as op # supported operators operators = {ast.Add: op.add, ast.Sub: … Read more

How to create the most compact mapping n → isprime(n) up to a limit N?

The fastest algorithm for general prime testing is AKS. The Wikipedia article describes it at lengths and links to the original paper. If you want to find big numbers, look into primes that have special forms like Mersenne primes. The algorithm I usually implement (easy to understand and code) is as follows (in Python): def … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)