def test_reshape(): m0 = eye(3) assert m0.reshape(1, 9) == Matrix(1, 9, (1, 0, 0, 0, 1, 0, 0, 0, 1)) m1 = Matrix(3, 4, lambda i, j: i + j) assert m1.reshape(4, 3) == Matrix( ((0, 1, 2), (3, 1, 2), (3, 4, 2), (3, 4, 5))) assert m1.reshape(2, 6) == Matrix(((0, 1, 2, 3, 1, 2), (3, 4, 2, 3, 4, 5)))
def getBasis(self, A, b): A = Matrix(A) b = Matrix(b) if b.shape != (A.shape[0], 1): raise Exception( "Length of b vector ({}) does not match number of rows" " in A matrix ({})".format(b.shape[0], A.shape[0])) G = zeros(A.shape[1] + 1, A.shape[0] + 1) G[:-1, :-1] = A.T G[-1, :-1] = b.reshape(1, b.shape[0]) G[-1, -1] = 1 # A is m x n, b is m x 1, solving AX=b, X is n x 1+ # Ab is the (n+1) x m transposed augmented matrix. G=[A^t|0] [b^t]1] hnf, P, rank = self.lllhermite(G) r = rank - 1 # For convenience if not any(chain(hnf[:r, -1], hnf[r, :-1])) and hnf[r, -1] == 1: nullity = hnf.shape[0] - rank if nullity: basis = P[rank:, :-1].col_join(-P[r, :-1]) solutions = basis else: return P[rank:, :-1].col_join(-P[r, :-1]) raise NotImplementedError( "Ax=B has unique solution in integers") else: solutions = [] return solutions
def printnp(m): """ Prints a sympy matrix in the same format as a numpy array """ m = Matrix(m) if m.shape[1] == 1: m = m.reshape(1, m.shape[0]) elem_len = max(num_chars(d) for d in m.vec()) if m.shape[0] > 1: outstr = '[[' else: outstr = '[' for i in xrange(m.shape[0]): if i: outstr += ' [' char_count = 0 for j, elem in enumerate(m[i, :]): char_count += elem_len if char_count > 77: outstr += '\n ' char_count = elem_len + 2 # Add spaces outstr += ' ' * (elem_len - num_chars(elem) + int(j != 0)) + str(elem) if i < m.shape[0] - 1: outstr += ']\n' if m.shape[0] > 1: outstr += ']]' else: outstr += ']' print outstr
def solve(self, A, b): """ Finds small solutions to systems of diophantine equations, A x = b, where A is a M x N matrix of coefficents, b is a M x 1 vector and x is the N x 1 solution vector, e.g. >>> from sympy import Matrix >>> import diophantine >>> Dio = Diophantine() >>> A = Matrix([[1, 0, 0, 2], [0, 2, 3, 5], [2, 0, 3, 1], [-6, -1, 0, 2], [0, 1, 1, 1], [-1, 2, 0,1], [-1, -2, 1, 0]]).T >>> b = Matrix([1, 1, 1, 1]) >>> Dio.solve(A, b) [Matrix([ [-1], [ 1], [ 0], [ 0], [-1], [-1], [-1]])] The returned solution vector will tend to be one with the smallest norms. If multiple solutions with the same norm are found they will all be returned. If there are no solutions the empty list will be returned. """ A = Matrix(A) b = Matrix(b) if b.shape != (A.shape[0], 1): raise Exception( "Length of b vector ({}) does not match number of rows" " in A matrix ({})".format(b.shape[0], A.shape[0])) G = zeros(A.shape[1] + 1, A.shape[0] + 1) G[:-1, :-1] = A.T G[-1, :-1] = b.reshape(1, b.shape[0]) G[-1, -1] = 1 # A is m x n, b is m x 1, solving AX=b, X is n x 1+ # Ab is the (n+1) x m transposed augmented matrix. G=[A^t|0] [b^t]1] hnf, P, rank = self.lllhermite(G) r = rank - 1 # For convenience if not any(chain(hnf[:r, -1], hnf[r, :-1])) and hnf[r, -1] == 1: nullity = hnf.shape[0] - rank if nullity: basis = P[rank:, :-1].col_join(-P[r, :-1]) solutions = self.get_solutions(basis) else: raise NotImplementedError( "Ax=B has unique solution in integers") else: solutions = [] return solutions
def solve(A, b): """ Finds small solutions to systems of diophantine equations, A x = b, where A is a M x N matrix of coefficents, b is a M x 1 vector and x is the N x 1 solution vector, e.g. >>> from sympy import Matrix >>> from diophantine import solve >>> A = Matrix([[1, 0, 0, 2], [0, 2, 3, 5], [2, 0, 3, 1], [-6, -1, 0, 2], [0, 1, 1, 1], [-1, 2, 0,1], [-1, -2, 1, 0]]).T >>> b = Matrix([1, 1, 1, 1]) >>> solve(A, b) [Matrix([ [-1], [ 1], [ 0], [ 0], [-1], [-1], [-1]])] The returned solution vector will tend to be one with the smallest norms. If multiple solutions with the same norm are found they will all be returned. If there are no solutions the empty list will be returned. """ A = Matrix(A) b = Matrix(b) if b.shape != (A.shape[0], 1): raise Exception("Length of b vector ({}) does not match number of rows" " in A matrix ({})".format(b.shape[0], A.shape[0])) G = zeros(A.shape[1] + 1, A.shape[0] + 1) G[:-1, :-1] = A.T G[-1, :-1] = b.reshape(1, b.shape[0]) G[-1, -1] = 1 # A is m x n, b is m x 1, solving AX=b, X is n x 1+ # Ab is the (n+1) x m transposed augmented matrix. G=[A^t|0] [b^t]1] hnf, P, rank = lllhermite(G) r = rank - 1 # For convenience if not any(chain(hnf[:r, -1], hnf[r, :-1])) and hnf[r, -1] == 1: nullity = hnf.shape[0] - rank if nullity: basis = P[rank:, :-1].col_join(-P[r, :-1]) solutions = get_solutions(basis) else: raise NotImplementedError("Ax=B has unique solution in integers") else: solutions = [] return solutions
def test_reshape(): m0 = eye(3) assert m0.reshape(1,9) == Matrix(1,9,(1,0,0,0,1,0,0,0,1)) m1 = Matrix(3,4, lambda i,j: i+j) assert m1.reshape(4,3) == Matrix(((0,1,2), (3,1,2), (3,4,2), (3,4,5))) assert m1.reshape(2,6) == Matrix(((0,1,2,3,1,2), (3,4,2,3,4,5)))
# lever arm R_B2B3 = rot_axis2(-phi_y) * rot_axis1(-phi_x) R_IB3 = R_IB2 * R_B2B3 r_S2S3 = R_IB3 * Matrix([0, 0, -l]) b_omega_3 = Matrix([phi_x_dot, 0, 0]) + rot_axis1(phi_x) * \ Matrix([0, phi_y_dot, 0]) + R_B2B3.T * b_omega_2 v_OS3 = v_OS2 + R_IB3 * (b_omega_3.cross(Matrix([0, 0, -l]))) # calculate Jacobians v_i = [v_OS1, v_OS2, v_OS3] om_i = [omega_1, b_omega_2, b_omega_3] ang_dot = Matrix([w_1z, psi_x_dot, psi_y_dot, phi_x_dot, phi_y_dot]) R_IB2_flat = R_IB2.reshape(9, 1) R_IB2_dot = ( R_IB2 * Matrix([[0, -w_2z, w_2y], [w_2z, 0, -w_2x], [-w_2y, w_2x, 0]])).reshape(9, 1) J_i = [v.jacobian(omega) for v in v_i] JR_i = [om.jacobian(omega) for om in om_i] # Impulse p_i = [m[i] * v_i[i] for i in range(3)] p_dot_i = [ p.jacobian(omega) * omega_dot + p.jacobian(ang) * ang_dot + p.jacobian(R_IB2_flat) * R_IB2_dot for p in p_i]
Return basis function. """ return x**i x = Symbol("x") q = input("Enter the value of q [3]: ") y0 = input("Enter the value of y0 [0]: ") basis = [x**i for i in range(q+1)] ci = [Symbol("ci_%i" %i) for i in range(q+1)] y = y0 + sum(ci[i]*basis[i] for i in range(1,q+1)) print("The trial solution is: ", u) k = Matrix([[0,0,0],[0,0,0],[0,0,0]]) f = Matrix(1, q, range(q)) for i in range(1,q+1): for j in range(1,q+1): k[i-1,j-1] = ci[i]*ci[j]*np.diff(basis[i], x)*np.diff(basis[j], x) for i in range(1,q+1): f[i-1]=2*x*ci[i]*basis[i]+ci[i] functional= i ntegrate(sum(k.reshape(1,q**2))+sum(f),[x,0,1]) s1 = Derivative(functional,ci[1]) d1 = s1.doit() s2 = Derivative(functional,ci[2]) d2 = s2.doit() s3 = Derivative(functional,ci[3]) d3 = s3.doit() xx = solve([d1,d2,d3],dict=True) yt = y.subs(xx) print("The Approximate Solution is: yt= ",yt) xr = np.linspace(0,1,100) yt = xr**3/6 - xr # The Approximate Solution
def ensureMat(x, nr, nc): """ Ensures that the input is a matrix of shape nr, nc""" if not isinstance(x, Matrix): x = Matrix(x) return x.reshape(nr, nc)
dsipp = [0.8, 2.8, 4.8, 6.8] for i in dsipp: displacement(i, 8.8) ## stress at nodal point E = 33.2 * 10**6 #KN/m2 I = 0.2916 #m4 disp3 = Matrix(([-0.00708], [0.000192], [0], [0.00131])) disp = Matrix(([0], [-0.00131], [-0.00686], [-0.000321])) disp1 = Matrix(([-0.00686], [-0.000321], [-0.00708], [0.000192])) # at 0m in the 1st beam element lz = 8 stress = E * I / lz**2 matri_0 = Matrix(([6, 4 * lz, -6, 2 * lz])) matri_011 = matri_0 matri_01 = matri_0.reshape(1, 4) print(" " * 100) # print('Nodal stress of the beam WRT nodal displacement at {}m '.format(lz), matri_01,matri_01.shape) # at 8m stress0 = stress * matri_01 # print(stress0,'\n',disp,disp.shape,'\n', matri_01,matri_01.shape) ans1 = stress0 * disp print(" " * 100) print('nodal stress @ point 0', ans1) matri_1 = Matrix(([-6, -2 * lz, 6, -4 * lz])) stress1 = (stress * matri_1).reshape(1, 4) ans2 = stress1 * disp print(" " * 100) print('corect at this point nodal stress @ point L', ans2)