def detresult():
    if request.method == 'POST':

        i = 0

        while str(request.form.get("num[" + str(i) + "][0]")) != 'None':
            i += 1

        dim = i

        matrix_list = []
        matrix_string = []

        for i in range(0, dim):
            row = []
            srow = []
            for j in range(0, dim):

                input = str(request.form.get("num[" + str(i) + "][" + str(j) + "]"))

                fraction = "-?\d+/\d+"

                try:
                    if re.match(fraction, input):
                        if re.match(fraction, input).span()[1] == len(input):

                            srow.append(input)

                            input = intvalue(input)
                            row.append(float(input))
                    else:

                        srow.append(input)
                        row.append(float(input))

                except ValueError:
                    return render_template('form.html', value_error="True", error="Value Error", message="You entered invalid values")

            matrix_string.append(srow)
            matrix_list.append(row)

        matrix = np.array(matrix_list)

        det = linalg.det(matrix)

        if np.abs(np.ceil(det) - det) < 0.000000000001:
            det = np.ceil(det)
        elif np.abs(det - np.floor(det) < 0.000000000001):
            det = np.floor(det)

        detstr = fractions.Fraction(det).limit_denominator()

        return render_template('form.html', choice="det", matrix=matrix_string, detstr=detstr, dim=dim)
示例#2
0
def eigresult():
    if request.method == 'POST':

        i = 0

        while str(request.form.get("matrix[" + str(i) + "][0]")) != 'None':
            i += 1

        dim = i

        matrix_list = []
        matrix_string = []

        for i in range(0, dim):
            row = []
            srow = []
            for j in range(0, dim):

                input = str(
                    request.form.get("matrix[" + str(i) + "][" + str(j) + "]"))

                fraction = "-?\d+/\d+"

                try:
                    if re.match(fraction, input):
                        if re.match(fraction, input).span()[1] == len(input):
                            srow.append(input)

                            input = intvalue(input)
                            row.append(float(input))
                    else:

                        srow.append(input)
                        row.append(float(input))

                except ValueError:
                    return render_template(
                        'form.html',
                        value_error="True",
                        error="Value Error",
                        message="You entered invalid values")

            matrix_string.append(srow)
            matrix_list.append(row)

        matrix = np.array(matrix_list)

        eigvaluesraw, eigvectorsraw = linalg.eig(matrix)

        eigvectorsraw = np.transpose(eigvectorsraw)

        eigenvalues = np.array(eigvaluesraw).tolist()
        eigenvectors = np.array(eigvectorsraw, dtype='float64').tolist()

        for i in range(0, len(list(eigenvalues))):
            if eigenvalues[i].imag != 0:
                return render_template(
                    'form.html',
                    choice="eig",
                    imaginary="true",
                    dim=dim,
                    matrixString=matrix_string,
                    error="Imaginary Solutions",
                    message=
                    "The solution to this eigenspace produces imaginary roots!"
                )
            else:
                eigenvalues[i] = fractions.Fraction(
                    eigenvalues[i].real).limit_denominator()

        for i in range(0, len(list(eigenvectors))):
            k = 0
            for j in range(0, len(eigenvectors[i])):
                eigenvectors[i][j] = fractions.Fraction(
                    eigenvectors[i][j]).limit_denominator()

                if eigenvectors[i][j] != 0:
                    k = fractions.Fraction(
                        numerator=eigenvectors[i][j].denominator,
                        denominator=eigenvectors[i]
                        [j].numerator).limit_denominator()

            if k != 0:
                eigenvectors[i] = np.array(eigenvectors[i]) * k

                for j in range(0, len(eigenvectors[i])):
                    if eigenvectors[i][j].denominator > 10000000:
                        eigenvectors[i][j] = fractions.Fraction(
                            eigenvectors[i][j].numerator /
                            eigenvectors[i][j].denominator).limit_denominator(
                                100)

        return render_template('form.html',
                               choice="eig",
                               matrixString=matrix_string,
                               eigenvalues=eigenvalues,
                               eigenspace=eigenvectors,
                               dim=dim,
                               num_eigenvalues=len(list(eigenvalues)))
def matmulresult():

    if request.method == 'POST':

        i = 0
        j = 0

        while str(request.form.get("matrixOne[" + str(i) + "][0]")) != 'None':
            i += 1

        m1d1 = i

        while str(request.form.get("matrixOne[0][" + str(j) + "]")) != 'None':
            j += 1

        m1d2 = j

        matrix_one_list = []
        matrix_one_string = []

        for i in range(0, m1d1):
            row = []
            srow = []

            for j in range(0, m1d2):

                input = str(
                    request.form.get("matrixOne[" + str(i) + "][" + str(j) +
                                     "]"))

                fraction = "\d+/\d+"

                try:
                    if re.match(fraction, input):
                        if re.match(fraction, input).span()[1] == len(input):

                            srow.append(input)

                            input = intvalue(input)

                            if np.abs(np.ceil(float(input)) -
                                      float(input)) < 0.000000000001:
                                input = np.ceil(input)
                            elif np.abs(
                                    float(input) -
                                    np.floor(float(input)) < 0.000000000001):
                                input = np.floor(float(input))

                            row.append(float(input))
                    else:
                        srow.append(input)

                        row.append(float(input))
                except ValueError:
                    return render_template(
                        'form.html',
                        value_error="True",
                        error="Value Error",
                        message="You entered invalid values")

            matrix_one_string.append(srow)
            matrix_one_list.append(row)

        i = 0
        j = 0

        while str(request.form.get("matrixTwo[" + str(i) + "][0]")) != 'None':
            i += 1

        m2d1 = i

        while str(request.form.get("matrixTwo[0][" + str(j) + "]")) != 'None':
            j += 1

        m2d2 = j

        matrix_two_list = []
        matrix_two_string = []

        for i in range(0, m2d1):
            row = []
            srow = []

            for j in range(0, m2d2):

                input = str(
                    request.form.get("matrixTwo[" + str(i) + "][" + str(j) +
                                     "]"))

                fraction = "-?\d+/\d+"

                try:
                    if re.match(fraction, input):
                        if re.match(fraction, input).span()[1] == len(input):

                            srow.append(input)

                            input = intvalue(input)

                            if np.abs(np.ceil(float(input)) -
                                      float(input)) < 0.000000000001:
                                input = np.ceil(input)
                            elif np.abs(
                                    float(input) -
                                    np.floor(float(input)) < 0.000000000001):
                                input = np.floor(float(input))

                            row.append(float(input))
                    else:
                        srow.append(input)

                        row.append(float(input))
                except ValueError:
                    return render_template(
                        'form.html',
                        value_error="True",
                        error="Value Error",
                        message="You entered invalid values")

            matrix_two_string.append(srow)
            matrix_two_list.append(row)

        matrix_one = np.array(matrix_one_list)
        matrix_two = np.array(matrix_two_list)

        matmulresult = np.matmul(matrix_one, matrix_two)
        resultdimensions = matmulresult.shape

        matmulresult = matmulresult.tolist()

        for i in range(0, resultdimensions[0]):

            for j in range(0, resultdimensions[1]):
                matmulresult[i][j] = fractions.Fraction(
                    matmulresult[i][j]).limit_denominator()

        if m2d2 == 1:
            return render_template('form.html',
                                   choice="matmul",
                                   columnvector="true",
                                   matmulresult=matmulresult,
                                   matrixOneString=matrix_one_string,
                                   matrixTwoString=matrix_two_string,
                                   m1_1=m1d1,
                                   m1_2=m1d2,
                                   m2_1=m2d1,
                                   m2_2=m2d2)

        return render_template('form.html',
                               choice="matmul",
                               matmulresult=matmulresult,
                               matrixOneString=matrix_one_string,
                               matrixTwoString=matrix_two_string,
                               m1_1=m1d1,
                               m1_2=m1d2,
                               m2_1=m2d1,
                               m2_2=m2d2)
def invresult():
    if request.method == 'POST':

        i = 0

        while str(request.form.get("matrix[" + str(i) + "][0]")) != 'None':
            i += 1

        dim = i

        matrix_list = []
        matrix_string = []

        for i in range(0, dim):
            row = []
            srow = []

            for j in range(0, dim):

                input = request.form.get("matrix[" + str(i) + "][" + str(j) +
                                         "]")

                fraction = "-?\d+/\d+"

                try:
                    if re.match(fraction, input):
                        if re.match(fraction, input).span()[1] == len(input):
                            srow.append(input)

                            input = intvalue(input)

                            if np.abs(np.ceil(float(input)) -
                                      float(input)) < 0.000000000001:
                                input = np.ceil(input)
                            elif np.abs(
                                    float(input) -
                                    np.floor(float(input)) < 0.000000000001):
                                input = np.floor(float(input))

                            row.append(float(input))

                    else:

                        srow.append(input)
                        row.append(float(input))
                except ValueError:
                    return render_template(
                        'form.html',
                        value_error="True",
                        error="Value Error",
                        message="You entered invalid values")

            matrix_string.append(srow)
            matrix_list.append(row)

        matrix = np.array(matrix_list)

        try:
            inv = np.linalg.inv(matrix)

            resultdimensions = inv.shape

            inv = inv.tolist()

            for i in range(0, resultdimensions[0]):

                for j in range(0, resultdimensions[1]):
                    inv[i][j] = fractions.Fraction(
                        inv[i][j]).limit_denominator()

        except np.linalg.LinAlgError:
            return render_template(
                'form.html',
                choice="inv",
                dim=dim,
                matrixString=matrix_string,
                singular="true",
                error="Singular Matrix",
                message=
                "The matrix you entered is singular, so the inverse does not exist!"
            )

        return render_template('form.html',
                               choice="inv",
                               matrixString=matrix_string,
                               inv=inv,
                               dim=dim)
def rrefresult():
    if request.method == 'POST':

        i = 0
        j = 0

        while str(request.form.get("matrix[" + str(i) + "][0]")) != 'None':
            i += 1

        dim1 = i

        while str(request.form.get("matrix[0][" + str(j) + "]")) != 'None':
            j += 1

        dim2 = j

        matrix_list = []
        matrix_string = []

        for i in range(0, dim1):
            row = []
            srow = []
            for j in range(0, dim2):

                input = str(
                    request.form.get("matrix[" + str(i) + "][" + str(j) + "]"))

                fraction = "-?\d+/\d+"

                try:
                    if re.match(fraction, input):
                        if re.match(fraction, input).span()[1] == len(input):
                            srow.append(input)

                            input = intvalue(input)

                            if np.abs(np.ceil(float(input)) -
                                      float(input)) < 0.000000000001:
                                input = np.ceil(input)
                            elif np.abs(
                                    float(input) -
                                    np.floor(float(input)) < 0.000000000001):
                                input = np.floor(float(input))

                            row.append(float(input))

                    else:

                        srow.append(input)
                        row.append(float(input))
                except ValueError:
                    return render_template(
                        'form.html',
                        value_error="True",
                        error="Value Error",
                        message="You entered invalid values")

            matrix_string.append(srow)
            matrix_list.append(row)

        matrix = sympy.Matrix(matrix_list)

        rref = np.array(matrix.rref()[0]).astype(np.float).tolist()

        for i in range(0, dim1):

            for j in range(0, dim2):
                rref[i][j] = fractions.Fraction(rref[i][j]).limit_denominator()

        return render_template('form.html',
                               choice="rref",
                               dim1=dim1,
                               dim2=dim2,
                               rref=rref,
                               matrix_string=matrix_string)