In [ ]:
# Init matplotlib

%matplotlib inline

import matplotlib

In [ ]:
FIG_SIZE = 4

def plot_linear_system(Ab, bounds=None, ax=None, color='r', alpha=1):
    """Plot the linear system defined by the extended matrix `Ab`."""
    
    # Check arguments #############
    
    assert Ab.ndim == 2
    assert Ab.shape[1] == 3
    
    if bounds is None:
        if ax is None:
            bounds = np.array([[-10, 10],
                               [-10, 10]])
        else:
            bounds = np.array(ax.axis()).reshape([2,2])
    
    assert bounds.ndim == 2
    assert bounds.shape == (2, 2)
    assert bounds[0,0] < bounds[0,1]
    assert bounds[1,0] < bounds[1,1]
    
    # Setup the plot ##############
    
    if ax is None:
        fig, ax = plt.subplots(figsize=(FIG_SIZE, FIG_SIZE))
        
    ax.axis('equal')

    ax.axis(bounds.flatten())
    ax.set_xticks(np.arange(*bounds[0], 1))
    ax.set_yticks(np.arange(*bounds[0], 1))

    ax.axhline(y=0, color='k')
    ax.axvline(x=0, color='k')

    # Compute points to plot ######

    x = bounds[0]            # horizontal axis limits
    
    Abnv = Ab[Ab[:,1] != 0]  # linear equations having a non-null x_2 coefficient (i.e. non-vertical line)
    Abv = Ab[Ab[:,1] == 0]   # linear equations having a null x_2 coefficient (i.e. vertical line)
    
    # Plot non-vertical lines #####
    
    yp1 = (Abnv[:,2] - Abnv[:,0] * x[0]) / Abnv[:,1]
    yp2 = (Abnv[:,2] - Abnv[:,0] * x[1]) / Abnv[:,1]
    plt.plot(x, np.array([yp1, yp2]), "-{}".format(color), alpha=alpha)
    
    # Plot vertical lines #########
    
    for ab in Abv:
        plt.axvline(ab[2] / ab[0], color=color, alpha=alpha)
    
    # TODO: add label (e.g. "2x + 3y = 1")

    plt.grid(True)
    
    return ax

In [ ]:
pre_str1 = r"""
$
\left\{
\begin{align}
"""

post_str1 = r"""\end{align}
\right.
$
"""

pre_str2 = r"""
$
\begin{pmatrix}
"""

post_str2 = r"""\end{pmatrix}
$
"""

def complete_matrix_to_latex(Ab):
    #6 x_1 + 7 x_2 &  = 11

    sys_str = ""
    
    for eq in Ab:
        
        eq_str = ""
        
        for coef_index, coef in enumerate(eq[:-1]):
            
            if coef != 0:
                if len(eq_str) > 0:
                    eq_str += "+ "
                if coef == 1:
                    eq_str += "x_{" + str(coef_index + 1) + "} "
                elif coef == -1:
                    eq_str += "-x_{" + str(coef_index + 1) + "} "
                else:
                    eq_str += str(coef) + " x_{" + str(coef_index + 1) + "} "
                        
        eq_str = eq_str.replace("+ -", "- ")
        eq_str += "& = {} \\\\\n".format(eq[-1])
        sys_str += eq_str
        
    sys_str = pre_str1 + sys_str + post_str1
    
    
    
    mat_str = ""
    
    for eq in Ab:
        
        eq_str = ""
        
        for coef_index, coef in enumerate(eq):
            
            if len(eq_str) > 0:
                eq_str += " & "
            eq_str += str(coef)
                        
        eq_str += "\\\\\n"
        mat_str += eq_str
        
    mat_str = pre_str2 + mat_str + post_str2
    
    return sys_str + "\n" + mat_str

Méthode par élimination (ou combinaison)

Exemple 1


In [ ]:
Ab = np.array([[ 1,  5,  7],
               [-2, -7, -5]])

print(complete_matrix_to_latex(Ab))
plot_linear_system(Ab);

In [ ]:
Ab = np.array([[1, 5, 7],
               [0, 3, 9]])

print(complete_matrix_to_latex(Ab))
plot_linear_system(Ab);

In [ ]:
Ab = np.array([[1, 5, 7],
               [0, 1, 3]])

print(complete_matrix_to_latex(Ab))
plot_linear_system(Ab);

In [ ]:
Ab = np.array([[1, 0, -8],
               [0, 1,  3]])

print(complete_matrix_to_latex(Ab))
plot_linear_system(Ab);