def log (a, b): try: try: return div(ln(a), ln(b)) except AttributeError: return div(math.log(a), math.log(b)) except ValueError: return nan
def bfgs_update(self, prev_inverse_hess, update_x, prev_grad, curr_grad): s = update_x y = curr_grad - prev_grad b = prev_inverse_hess t1 = np.div(np.dot(s, y.T), np.dot(y.T, s)) t2 = np.div(np.dot(y, s.T), np.dot(y.T, s)) t3 = np.div(np.dot(s, s.T), np.dot(y.T, s)) t1 = np.eye(t1.shape) - t1 t2 = np.eye(t2.shape) - t2 curr_inverse_hess = np.dot(np.dot(t1, b), t2) + t3 update_val = -np.dot(curr_inverse_hess, curr_grad) return update_val, curr_inverse_hess
def xyz2radec(xyz, deg=True): "Transform heliocentric ICRF coordinates to RA DEC" if (xyz.ndim == 1): r = np.linalg.norm(xyz) RA = np.atan2(xyz[:, 1], xyz[:, 0]) DEC = np.arccos(np.div(xyz[:, 2], r)) elif (xyz.ndim == 2): r = np.linalg.norm(xyz, axis=1) RA = np.atan2(xyz[:, 1], xyz[:, 0]) DEC = np.arccos(np.div(xyz[:, 2], r)) else: raise TypeError if (deg): RA_out = np.rad2deg(RA) DEC_out = np.rad2deg(DEC) else: RA_out = RA DEC_out = DEC return RA_out, DEC_out
def parseExpr(expr): if expr.data == "add": return add(parseExpr(expr.children[0]), parseExpr(expr.children[1])) elif expr.data == "sub": return sub(parseExpr(expr.children[0]), parseExpr(expr.children[1])) elif expr.data == "mul": return mul(parseExpr(expr.children[0]), parseExpr(expr.children[1])) elif expr.data == "div": return div(parseExpr(expr.children[0]), parseExpr(expr.children[1])) elif expr.data == "neg": return neg(parseExpr(expr.children[0])) elif expr.data == "word": word = expr.children[0].value.lower() if word in w2idx.keys(): return mat[w2idx[word]] else: return np.zeros(100)
def math_div(self, x1, x2, out=None): return np.div(x1, x2, out=out)
def show_mat(x): print(matrix_to_latex(x)) # edit this, then run the program f = frac_tag s = sqrt_tag ln = lambda s: "ln(" + str(s) + ")" mat = [[0.024157], [0.024363], [-0.083488], [-0.342448]] show_mat(mat) from numpy import subtract as sub, add, multiply as mul, divide as div # Defines functions and targets funcs = [ [lambda r: sub(r, mat[3]), lambda r: sub(r, mat[3])], [lambda r: div(r, 2), lambda r: sub(r, mat[0])], [lambda r: sub(r, mat[1])], [lambda r: sub(r, mul(3/2, mat[2])), lambda r: add(r, mul(3/2, mat[2]))], [lambda r: add(r, mul(2, mat[3])), lambda r: sub(r, mul(3, mat[3])), lambda r: add(r, mul(2, mat[3])), lambda r: mul(r, 2)] ] tgts = [ [0, 2], [1, 3], [3], [1, 3], [0, 1, 2, 3] ] assert len(funcs) == len(tgts) for i in range(len(funcs)): assert len(funcs[i]) == len(tgts[i])
# Author: Bojan G. Kalicanin # Date: 01-Dec-2016 # You are given two arrays (A & B) of dimensions NxM. # Your task is to perform the following operations: # # 1. Add(A + B) # 2. Subtract(A - B) # 3. Multiply(A * B) # 4. Divide (A / B) # 5. Mod(A % B) # Power (A ** B) import numpy n, m = map(int, input().split()) a = numpy.array([map(int, input().split()) for i in range(n)]) b = numpy.array([map(int, input().split()) for i in range(n)]) print(a + b) print(a - 1) print(a * b) print(numpy.div(A, B)) print(a % b) print(a**b)