def test_add(self): mtx1 = mat.Matrix(3, 3) mtx2 = mat.Matrix(3, 3) mtx1.populate(np.linspace(1, 9, 9)) mtx2.populate(np.linspace(10, 19, 9)) mtx1.add(mtx2) np.testing.assert_array_equal(mtx1.values, np.linspace(11, 28, 9).reshape(3, 3))
def test_mult(self): mtx1 = mat.Matrix(3, 3) mtx2 = mat.Matrix(3, 3) mtx1.populate(np.linspace(1, 9, 9)) mtx2.populate(np.linspace(10, 19, 9)) prod = np.matmul(mtx1.values, mtx2.values) mtx1.mult(mtx2) np.testing.assert_array_equal(mtx1.values, prod)
def test_LUdecomp(self): import scipy.linalg mtx = mat.Matrix(3, 3) mtx.populate([2, -1, -2, -4, 6, 3, -4, -2, 8]) l, u = mtx.ludecomp() l.mult(u) np.testing.assert_array_equal(l.values, mtx.values)
def rot_v(v, alpha, beta, gamma, delta, rho, epsilon): # XY Plane rot matrix mxy = [ [ math.cos(alpha), math.sin(alpha), 0, 0], [ -math.sin(alpha), math.cos(alpha), 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1] ] mxy = mat.SquareMatrix(mxy) # YZ Plane rot matrix myz = [ [ 1, 0, 0, 0], [ 0, math.cos(beta), math.sin(beta), 0], [ 0, -math.sin(beta), math.cos(beta), 0], [ 0, 0, 0, 1] ] myz = mat.SquareMatrix(myz) # ZX Plane rot matrix mzx = [ [ math.cos(gamma), 0, -math.sin(gamma), 0], [ 0, 1, 0, 0], [ math.sin(gamma), 0, math.cos(gamma), 0], [ 0, 0, 0, 1] ] mzx = mat.SquareMatrix(mzx) # XW Plane rot matrix mxw = [ [ math.cos(delta), 0, 0, math.sin(delta)], [ 0, 1, 0, 0], [ 0, 0, 1, 0], [ -math.sin(delta), 0, 0, math.cos(delta)] ] mxw = mat.SquareMatrix(mxw) # YW Plane rot matrix myw = [ [ 1, 0, 0, 0], [ 0, math.cos(rho), 0, -math.sin(rho)], [ 0, 0, 1, 0], [ 0, math.sin(rho), 0, math.cos(rho)] ] myw = mat.SquareMatrix(myw) # ZW Plane rot matrix mzw = [ [ 1, 0, 0, 0], [ 0, 1, 0, 0], [ 0, 0, math.cos(epsilon), -math.sin(epsilon)], [ 0, 0, math.sin(epsilon), math.cos(epsilon)] ] mzw = mat.SquareMatrix(mzw) # multiply them mres = mxy * myz * mzx * mxw * myw * mzw mv = mat.Matrix(v.coords) # creates a 1 column matrix r = mres * mv return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_beta(v, beta): myz = [ [ 1, 0, 0, 0], [ 0, math.cos(beta), math.sin(beta), 0], [ 0, -math.sin(beta), math.cos(beta), 0], [ 0, 0, 0, 1] ] myz = mat.SquareMatrix(myz) r = myz * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_epsilon(v, epsilon): mzw = [ [ 1, 0, 0, 0], [ 0, 1, 0, 0], [ 0, 0, math.cos(epsilon), -math.sin(epsilon)], [ 0, 0, math.sin(epsilon), math.cos(epsilon)] ] mzw = mat.SquareMatrix(mzw) r = mzw * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_rho(v, rho): myw = [ [ 1, 0, 0, 0], [ 0, math.cos(rho), 0, -math.sin(rho)], [ 0, 0, 1, 0], [ 0, math.sin(rho), 0, math.cos(rho)] ] myw = mat.SquareMatrix(myw) r = myw * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_delta(v, delta): mxw = [ [ math.cos(delta), 0, 0, math.sin(delta)], [ 0, 1, 0, 0], [ 0, 0, 1, 0], [ -math.sin(delta), 0, 0, math.cos(delta)] ] mxw = mat.SquareMatrix(mxw) r = mxw * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_gamma(v, gamma): mzx = [ [ math.cos(gamma), 0, -math.sin(gamma), 0], [ 0, 1, 0, 0], [ math.sin(gamma), 0, math.cos(gamma), 0], [ 0, 0, 0, 1] ] mzx = mat.SquareMatrix(mzx) r = mzx * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def rot_alpha(v, alpha): mxy = [ [ math.cos(alpha), math.sin(alpha), 0, 0], [ -math.sin(alpha), math.cos(alpha), 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1] ] mxy = mat.SquareMatrix(mxy) r = mxy * mat.Matrix(v.coords) return vec.V4(r[0,0], r[1,0], r[2,0], r[3,0])
def test_inverse(self): mtx = mat.Matrix(2, 2) mtx.populate([2, 3, 2, 2]) check = np.linalg.inv(mtx.values) mtx.invert() np.testing.assert_array_equal(mtx.values, check)
def test_trace(self): mtx = mat.Matrix(3, 3) mtx.populate(np.linspace(1, 9, 9)) np.testing.assert_array_equal(mtx.trace(), np.trace(mtx.values))
def test_transpose(self): mtx = mat.Matrix(3, 3) mtx.populate(np.linspace(1, 9, 9)) mtx.transpose() np.testing.assert_array_equal( mtx.values, np.transpose(np.linspace(1, 9, 9).reshape(3, 3)))
def xsq(x): return x**2 xs = np.linspace(1, 10, 10) print(nc.symm_deriv(xsq, xs)) print(nc.riemann_integral(xsq, xs, left=True)) print(nc.riemann_integral(xsq, xs, right=True)) print(nc.riemann_integral(xsq, xs, midpoint=True)) print(nc.trap_integral(xsq, xs)) print(nc.simp_integral(xsq, xs)) a = mat.Matrix(2, 5) a.transpose() a.transpose() a.populate(np.linspace(1, 10, 10)) b = mat.Matrix(2, 5) a.add(b) c = mat.Matrix(2, 5) c.populate(np.linspace(1, 10, 10)) d = mat.Matrix(5, 2) d.populate(np.linspace(1, 10, 10)) a.mult(d) a.transpose() print(a.trace()) a = mat.Matrix(3, 3) a.populate([2, -1, -2, -4, 6, 3, -4, -2, 8])
def test_init(self): mtx = mat.Matrix(2, 2) np.testing.assert_array_equal(mtx.values, np.zeros((2, 2)))
def test_det(self): mtx = mat.Matrix(3, 3) mtx.populate(np.linspace(1, 9, 9)) self.assertEqual(mtx.determinant(), np.linalg.det(mtx.values))
plt.rc('font', size=26) plt.rc('lines', linewidth=3) # --- Start of Task 1 --- # # See test_mat.py # # --- End of Task 1 --- # --- Start of Task 2 --- l, u, Aul = np.loadtxt('A_coefficients.dat', unpack=True, delimiter=',') # load coefficients for A_ul l, u = l.astype(int), u.astype(int) # ensure ints for indexing coeffs_aul = mat.Matrix(9, 9) for value in range(Aul.shape[0]): coeffs_aul.values[l[value] - 1, u[value] - 1] = Aul[ value] # subtract 1 from indices to go from matrix (1, 2, ...) to Python (0, 1, ...) indexing coeffs_aul.values = coeffs_aul.values[:-1, 1:] # cut off the last row since lower goes from 1-8, cut off first col since upper goes from 2-9 # note: when computing energies/frequencies, make sure to add +2 to column index and +1 to row index for physically meaningful representation of upper/lower levels def deltaE(i, j): ''' For deltaE_ul, i = col+2, j = row+1, otherwise flip i and j. ''' return -13.6 * (1. / i**2 - 1. / j**2)