Example #1
0
File: kane.py Project: royels/sympy
    def _initialize_kindiffeq_matrices(self, kdeqs):
        """Initialize the kinematic differential equation matrices."""

        if kdeqs:
            if len(self._q) != len(kdeqs):
                raise ValueError('There must be an equal number of kinematic '
                                 'differential equations and coordinates.')
            kdeqs = Matrix(kdeqs)

            u = self._u
            qdot = self._qdot
            # Dictionaries setting things to zero
            u_zero = dict((i, 0) for i in u)
            uaux_zero = dict((i, 0) for i in self._uaux)
            qdot_zero = dict((i, 0) for i in qdot)

            f_k = kdeqs.subs(u_zero).subs(qdot_zero)
            k_ku = (kdeqs.subs(qdot_zero) - f_k).jacobian(u)
            k_kqdot = (kdeqs.subs(u_zero) - f_k).jacobian(qdot)

            f_k = k_kqdot.LUsolve(f_k)
            k_ku = k_kqdot.LUsolve(k_ku)
            k_kqdot = eye(len(qdot))

            self._qdot_u_map = solve_linear_system_LU(
                Matrix([k_kqdot.T, -(k_ku * u + f_k).T]).T, qdot)

            self._f_k = f_k.subs(uaux_zero)
            self._k_ku = k_ku.subs(uaux_zero)
            self._k_kqdot = k_kqdot
        else:
            self._qdot_u_map = None
            self._f_k = Matrix()
            self._k_ku = Matrix()
            self._k_kqdot = Matrix()
Example #2
0
File: kane.py Project: Lenqth/sympy
    def _initialize_kindiffeq_matrices(self, kdeqs):
        """Initialize the kinematic differential equation matrices."""

        if kdeqs:
            if len(self.q) != len(kdeqs):
                raise ValueError('There must be an equal number of kinematic '
                                 'differential equations and coordinates.')
            kdeqs = Matrix(kdeqs)

            u = self.u
            qdot = self._qdot
            # Dictionaries setting things to zero
            u_zero = dict((i, 0) for i in u)
            uaux_zero = dict((i, 0) for i in self._uaux)
            qdot_zero = dict((i, 0) for i in qdot)

            f_k = msubs(kdeqs, u_zero, qdot_zero)
            k_ku = (msubs(kdeqs, qdot_zero) - f_k).jacobian(u)
            k_kqdot = (msubs(kdeqs, u_zero) - f_k).jacobian(qdot)

            f_k = k_kqdot.LUsolve(f_k)
            k_ku = k_kqdot.LUsolve(k_ku)
            k_kqdot = eye(len(qdot))

            self._qdot_u_map = solve_linear_system_LU(
                    Matrix([k_kqdot.T, -(k_ku * u + f_k).T]).T, qdot)

            self._f_k = msubs(f_k, uaux_zero)
            self._k_ku = msubs(k_ku, uaux_zero)
            self._k_kqdot = k_kqdot
        else:
            self._qdot_u_map = None
            self._f_k = Matrix()
            self._k_ku = Matrix()
            self._k_kqdot = Matrix()
Example #3
0
 def kindiffdict(self):
     """Returns the qdot's in a dictionary. """
     if self._k_kqdot is None:
         raise ValueError('Kin. diff. eqs need to be supplied first.')
     sub_dict = solve_linear_system_LU(Matrix([self._k_kqdot.T,
         -(self._k_ku * Matrix(self._u) + self._f_k).T]).T, self._qdot)
     return sub_dict
Example #4
0
File: kane.py Project: tuhina/sympy
 def kindiffdict(self):
     """Returns the qdot's in a dictionary. """
     if self._k_kqdot == None:
         raise ValueError('Kin. diff. eqs need to be supplied first')
     sub_dict = solve_linear_system_LU(Matrix([self._k_kqdot.T,
         -(self._k_ku * Matrix(self._u) + self._f_k).T]).T, self._qdot)
     return sub_dict
Example #5
0
    def _kindiffeq(self, kdeqs):
        """Supply all the kinematic differential equations in a list.

        They should be in the form [Expr1, Expr2, ...] where Expri is equal to
        zero

        Parameters
        ==========

        kdeqs : list (of Expr)
            The listof kinematic differential equations

        """
        if len(self._q) != len(kdeqs):
            raise ValueError('There must be an equal number of kinematic '
                             'differential equations and coordinates.')

        uaux = self._uaux
        # dictionary of auxiliary speeds which are equal to zero
        uaz = dict(list(zip(uaux, [0] * len(uaux))))

        #kdeqs = Matrix(kdeqs).subs(uaz)
        kdeqs = Matrix(kdeqs)

        qdot = self._qdot
        qdotzero = dict(list(zip(qdot, [0] * len(qdot))))
        u = self._u
        uzero = dict(list(zip(u, [0] * len(u))))

        f_k = kdeqs.subs(uzero).subs(qdotzero)
        k_kqdot = (kdeqs.subs(uzero) - f_k).jacobian(Matrix(qdot))
        k_ku = (kdeqs.subs(qdotzero) - f_k).jacobian(Matrix(u))

        self._k_ku = _mat_inv_mul(k_kqdot, k_ku)
        self._f_k = _mat_inv_mul(k_kqdot, f_k)
        self._k_kqdot = eye(len(qdot))
        self._qdot_u_map = solve_linear_system_LU(
            Matrix([
                self._k_kqdot.T, -(self._k_ku * Matrix(self._u) + self._f_k).T
            ]).T, self._qdot)

        self._k_ku = _mat_inv_mul(k_kqdot, k_ku).subs(uaz)
        self._f_k = _mat_inv_mul(k_kqdot, f_k).subs(uaz)
Example #6
0
    def _kindiffeq(self, kdeqs):
        """Supply all the kinematic differential equations in a list.

        They should be in the form [Expr1, Expr2, ...] where Expri is equal to
        zero

        Parameters
        ==========

        kdeqs : list (of Expr)
            The listof kinematic differential equations

        """
        if len(self._q) != len(kdeqs):
            raise ValueError('There must be an equal number of kinematic '
                             'differential equations and coordinates.')

        uaux = self._uaux
        # dictionary of auxiliary speeds which are equal to zero
        uaz = dict(list(zip(uaux, [0] * len(uaux))))

        #kdeqs = Matrix(kdeqs).subs(uaz)
        kdeqs = Matrix(kdeqs)

        qdot = self._qdot
        qdotzero = dict(list(zip(qdot, [0] * len(qdot))))
        u = self._u
        uzero = dict(list(zip(u, [0] * len(u))))

        f_k = kdeqs.subs(uzero).subs(qdotzero)
        k_kqdot = (kdeqs.subs(uzero) - f_k).jacobian(Matrix(qdot))
        k_ku = (kdeqs.subs(qdotzero) - f_k).jacobian(Matrix(u))

        self._k_ku = self._mat_inv_mul(k_kqdot, k_ku)
        self._f_k = self._mat_inv_mul(k_kqdot, f_k)
        self._k_kqdot = eye(len(qdot))
        self._qdot_u_map = solve_linear_system_LU(Matrix([self._k_kqdot.T,
            -(self._k_ku * Matrix(self._u) + self._f_k).T]).T, self._qdot)

        self._k_ku = self._mat_inv_mul(k_kqdot, k_ku).subs(uaz)
        self._f_k = self._mat_inv_mul(k_kqdot, f_k).subs(uaz)
nVals = [i for i in range(500, 500*10, 100)]
profits = []
for nval in nVals:
    profits.append(profitEq.evalf(subs={x: xVal, y: yVal, n:nval}))
py.plot(nVals, profits)
py.xlabel("Number of clothes")
py.ylabel("Profits")
py.show()
#d
#LU Decomposition
matrix = [[1, 2, 3, 10], 
          [4, 7, 5, 4], 
          [7, 1, 9, 11]]
intervals = []
start = time.time()
sp.solve_linear_system_LU(sp.Matrix(matrix), [x, y, n])
end = time.time()
intervals.append(end - start)
#Gauss Elimination
#According to SymPy, Gauss Elimination is more efficient than Gauss-Jordan elimination
start = time.time()
sp.solve_linear_system(sp.Matrix(matrix), x, y, n)
end = time.time()
intervals.append(end - start)
print("d.")
print("LU Decomposition (time taken):", intervals[0])
print("Gauss Elimination (time taken):", intervals[1])
print()

#No 2
#a
Example #8
0
    for a in iVal[0].args:
        co = a.args[0]
        print(co)
        var = a.args[1]
        index = 0
        for b in range(len(ys)):
            if (str(var) == str(ys[b])):
                index = b
                break

        sub[index] = co
    cMatrix.append(sub)

#Append the right hand side of the equation
curX = 0
for i in range(len(cMatrix)):
    cMatrix[i].append(eqx.evalf(subs={x: curX}))
    print("curX:", curX)
    curX = curX + xDelta

print()

for i in cMatrix:
    print(i)

#Convert to Sympy Matrix
print()
system = sp.Matrix(cMatrix)
result = sp.solve_linear_system_LU(system, ys)
print(result)
import sympy
from sympy.matrices import *
from sympy import pprint

M = Matrix([[5, 2, 8, 4], [1, 8, 6, 11], [2, 1, 2, 1]])

x, y, z = sympy.symbols('xyz')

a = sympy.solve_linear_system_LU(M, (x, y, z))

    def linear_and_non_equations(L='[]', var='[]', a=0, b=0, c=0, d=0, e=0, f=0, kind=0):
        if kind == 1:
            # Linear equations
            d = solve_linear_system_LU(sp.Matrix(L), parse_expr(var))
            for i in d:
                d[i] = d[i].evalf()
            return str(d)
        elif kind == 2:
            # Quadratic equations
            try:
                L = solve(a * x ** 2 + b * x + c - d, x)
                flag = 0
                if len(L) == 1:
                    # The equation has a repeated root
                    a = str(L[0])
                    for i in a:
                        if i.isalpha():
                            L[0] = L[0].evalf()
                            flag = 1
                    if flag == 0:
                        return 'The repeated root is: {:}'.format(L[0])
                    else:
                        return 'The repeated root is: {:s}'.format(functions.float_answer_analyzer(L[0]))
                elif len(L) != 1 and not functions.complex_checker(L[0]):
                    for i in L:
                        a = str(i)
                        for j in a:
                            if j.isalpha() and j != 'j':
                                L[0] = L[0].evalf();
                                L[1] = L[1].evalf()
                                flag = 1
                    # Equation with two distinct roots
                    if flag == 0:
                        return 'The roots are: {:} and {:}'.format(re(L[0]), re(L[1]))
                    else:
                        return 'The roots are: {:s} and {:s}'.format(functions.float_answer_analyzer(re(L[0])),
                                                                       functions.float_answer_analyzer(re(L[1])))
                else:
                    for i in L:
                        a = str(i)
                        for j in a:
                            if j.isalpha() and j != 'j':
                                L[0] = L[0].evalf();
                                L[1] = L[1].evalf()
                                flag = 1
                    # Equation with complex roots
                    if flag == 0:
                        return 'The roots are: {:}+{:}j and  {:}-{:}j'.format(re(L[0]), im(L[0]), re(L[1]), im(L[1]))
                    else:
                        return 'The roots are: {:s}+{:s}j and  {:s}-{:s}j'.format(
                            functions.float_answer_analyzer(re(L[0])), functions.float_answer_analyzer(Abs(im(L[0]))),
                            functions.float_answer_analyzer(re(L[1])), functions.float_answer_analyzer(Abs(im(L[1]))))
            except:
                return 'Math error!'
        elif kind == 3:
            # Cubic Equations
            try:
                l = solve(a * x ** 3 + b * x ** 2 + c * x + d - e, x)
                L = []
                for i in l:
                    a = i.evalf()
                    L.append(a)
                ans = []
                for i in L:
                    b = str(i)
                    for j in b:
                        if j == 'I':
                            ans.append('{:s}{:1s}{:s}j'.format(functions.float_answer_analyzer(re(i)),
                                                               '+' if im(i) > 0 else '-',
                                                               functions.float_answer_analyzer(Abs(im(i)))))
                            break

                    else:
                        ans.append('{:s}'.format(functions.float_answer_analyzer(i)))
                return 'The roots are: {:s},\n{:s}\nand {:s}'.format(ans[0], ans[1], ans[2])
            except:
                return 'Math error!'
        elif kind == 4:
            # Quartic equations
            try:
                l = solve(a * x ** 4 + b * x ** 3 + c * x ** 2 + d * x + e - f, x)
                L = []
                for i in l:
                    a = i.evalf()
                    L.append(a)
                ans = []
                for i in L:
                    b = str(i)
                    for j in b:
                        if j == 'I':
                            ans.append('{:s}{:1s}{:s}j'.format(functions.float_answer_analyzer(re(i)),
                                                               '+' if im(i) > 0 else '-',
                                                               functions.float_answer_analyzer(Abs(im(i)))))
                            break

                    else:
                        ans.append('{:s}'.format(functions.float_answer_analyzer(i)))
                return 'The roots are: {:s},\n{:s},\n{:s}\nand {:s}'.format(ans[0], ans[1], ans[2], ans[3])
            except:
                return 'Math error!'