# 1d3v electron hybrid code for R/L-waves with stationary ions and homogeneoues background field

## 1. The model
The electron hybrid model for cold electrons with linearized fluid equations and hot kinetic electrons (Vlasov equation) reads

\begin{align}
&\frac{\partial\tilde{\mathbf{j}}_\mathrm{c}}{\partial t}=\epsilon_0\Omega_\mathrm{pe}^2\tilde{\mathbf{E}}+\tilde{\mathbf{j}}_\mathrm{c}\times\mathbf{\Omega}_\mathrm{ce}(\mathbf{x}),\\
&\frac{\partial f_\mathrm{h}}{\partial t}+\mathbf{v}\cdot\nabla f_\mathrm{h}+\frac{q_\mathrm{e}}{m_\mathrm{e}}(\mathbf{E}+\mathbf{v}\times\mathbf{B})\cdot\nabla_\mathbf{v}f_\mathrm{h}=0,\\
&\frac{\partial \tilde{\mathbf{B}}}{\partial t}=-\nabla\times\tilde{\mathbf{E}},\\
&\frac{1}{c^2}\frac{\partial \tilde{\mathbf{E}}}{\partial t}=\nabla\times\tilde{\mathbf{B}}-\mu_0\tilde{\mathbf{j}}_\mathrm{c}-\mu_0q_\mathrm{e}\int\mathbf{v}\tilde{f}_\mathrm{h}\,\mathrm{d}^3\mathbf{v},
\end{align}

where $\mathbf{\Omega}_\mathrm{ce}(\mathbf{x})=q_\mathrm{e}\mathbf{B}_0(\mathbf{x})/m_\mathrm{e}$ is the oriented electron cyclotron frequency corresponding to the background field $\mathbf{B}_0=\mathbf{B}_0(\mathbf{x})$ and $\Omega_{\mathrm{pe}}^2=n_{\mathrm{c}0}e^2/\epsilon_0m_\mathrm{e}$ the plasma frequency of the cold electrons. Here, only wave propagation parallel to the background magnetic field $\mathbf{B}_0=\mathbf{B}_0(\mathbf{x})$ is considered, i.e. $\textbf{k}=k\textbf{e}_z$. Therefore the nabla operator is simply $\nabla=\textbf{e}_z\partial_z$. The background magnetic field simply is $B(\mathbf{x})=B_0\mathbf{e}_z$.

We use periodic boundary conditions for all quantities.

```python
import numpy as np
import scipy as sc
import matplotlib.pyplot as plt

import bsplines as bsp

import time

import utilitis_opt as utils_opt
import utilitis_pic_GEM

#====================================================================================
#  calling epyccel
#====================================================================================
from pyccel.epyccel import epyccel
utils_pic_fast = epyccel(utilitis_pic_GEM)
print('pyccelization of pic functions done!')
#====================================================================================

#===== saving data? (save = 1: yes, save = 0: no). If yes, name directory ===========
save = 0
title = 'test.txt'
#====================================================================================

#===== save only every saving_step-th time step =====================================
saving_step = 1
#====================================================================================

#===== physical parameters ==========================================================
eps0 = 1.0                         # vacuum permittivity
mu0 = 1.0                          # vacuum permeability
c = 1.0                            # speed of light
qe = -1.0                          # electron charge
me = 1.0                           # electron mass
B0z = 1.0                          # minimum of background magnetic field in z-direction
wce = qe*B0z/me                    # electron cyclotron frequency
wpe = 2*np.abs(wce)                # cold electron plasma frequency
nuh = 0.06                         # ratio of cold/hot electron densities (nh/nc)
nh = nuh*wpe**2                    # hot electron density
wpar = 0.2*c                       # parallel thermal velocity of energetic particles
wperp = 0.53*c                     # perpendicular thermal velocity of energetic particles
#====================================================================================

#===== initial conditions ===========================================================
k = 2.                             # wavenumber of initial wave field perturbations
amp = 1e-4                         # amplitude of initial wave field perturbations
eps = 0.                           # amplitude of spatial pertubation of initial distribution function

Ex0 = lambda z : 0*z               # initial Ex
Ey0 = lambda z : 0*z               # initial Ey
Bx0 = lambda z : amp*np.sin(k*z)   # initial Bx
By0 = lambda z : 0*z               # initial By
jx0 = lambda z : 0*z               # initial jcx
jy0 = lambda z : 0*z               # initial jcy
#====================================================================================

#===== numerical parameters =========================================================
Lz = 2*np.pi/k                     # length of z-domain
Nel = 32                           # number of elements z-direction
T = 200                            # simulation time
dt = 0.05                          # time step
p = 3                              # degree of B-spline basis functions in V0
Np = np.int(1e5)                   # number of markers
control = 1                        # control variate for noise reduction? (1: yes, 0: no)
time_integr = 1                    # do time integration? (1 : yes, 0: no)
#====================================================================================

#===== create parameter list ========================================================
pa = np.zeros(1*Nel + 5)

pa[0]  = eps0
pa[1]  = mu0
pa[2]  = c
pa[3]  = qe
pa[4]  = me
pa[5]  = B0z
pa[6]  = wce
pa[7]  = wpe
pa[8]  = nuh
pa[9]  = nh
pa[10] = wpar
pa[11] = wperp
pa[12] = k
pa[13] = amp
pa[14] = eps
pa[15] = Lz
pa[16] = Nel
pa[17] = T
pa[18] = dt
pa[19] = p
pa[20] = Np
pa[21] = control
pa[22] = saving_step
#====================================================================================

#===== discretization of spatial domain =============================================
dz   = Lz/Nel                                # element size
el_b = np.linspace(0, Lz, Nel + 1)           # element boundaries

Nbase   = Nel + p                            # total number of basis function
Nbase_0 = Nel                                # number of degrees of freedom
#====================================================================================

#===== some diagnostic values =======================================================
Eh_eq = Lz*nh*me/2*(wpar**2 + 2*wperp**2)    # equilibrium energetic electron energy

en_E = np.array([])                          # electric field energy
en_B = np.array([])                          # magnetic field energy
en_C = np.array([])                          # cold plasma energy
en_H = np.array([])                          # energetic electron energy
#====================================================================================

#===== initial energetic electron distribution function =============================
fh0 = lambda z, vx, vy, vz : (1 + eps*np.cos(k*z))*nh/((2*np.pi)**(3/2)*wpar*wperp**2)*np.exp(-vz**2/(2*wpar**2) - (vx**2 + vy**2)/(2*wperp**2))
#====================================================================================

#===== Maxwellian for control variate ===============================================
maxwell = lambda vx, vy, vz : nh/((2*np.pi)**(3/2)*wpar*wperp**2)*np.exp(-vz**2/(2*wpar**2) - (vx**2 + vy**2)/(2*wperp**2))
#====================================================================================

#===== sampling distribution for initial markers ====================================
g_sampling = lambda vx, vy, vz : 1/((2*np.pi)**(3/2)*wpar*wperp**2)*np.exp(-vz**2/(2*wpar**2) - (vx**2 + vy**2)/(2*wperp**2))*1/Lz
#====================================================================================

#===== spline knot vector, global mass matrices (in V0 and V1) and gradient matrix ==
Tz = bsp.make_knots(el_b, p, True)
tz = Tz[1:-1]

M0, C0 = utils_opt.matrixAssembly_V0(p, Nbase, Tz, True)
M1 = utils_opt.matrixAssembly_V1(p, Nbase, Tz, True)

G = utils_opt.GRAD_1d(p, Nbase, True)

print('matrix assembly done!')
#====================================================================================

#===== reserve memory for unknowns ==================================================
ex = np.empty(Nbase_0)
ey = np.empty(Nbase_0)
bx = np.empty(Nbase_0)
by = np.empty(Nbase_0)
yx = np.empty(Nbase_0)
yy = np.empty(Nbase_0)

z_old = np.empty(Np)
#====================================================================================

#===== initial coefficients with commuting projectors ===============================
proj = utils_opt.projectors_1d(p, Nbase, Tz, True)

ex[:] = proj.PI_0(Ex0)
ey[:] = proj.PI_0(Ey0)
bx[:] = proj.PI_1(Bx0)
by[:] = proj.PI_1(By0)
yx[:] = proj.PI_0(jx0)
yy[:] = proj.PI_0(jy0)

uj = np.concatenate((ex, ey, bx, by, yx, yy))

print('projection of initial fields done!')
#====================================================================================

#===== construct block matrices for field update ====================================
ZERO = np.zeros((Nbase_0, Nbase_0))

A1 = np.diag(np.ones(6*Nbase_0))
A1[0:Nbase_0, 0:Nbase_0] = M0
A1[Nbase_0:2*Nbase_0, Nbase_0:2*Nbase_0] = M0

A2 = np.block([[ZERO, ZERO, ZERO, c**2*np.dot(G.T, M1), -mu0*c**2*M0, ZERO], [ZERO, ZERO, -c**2*np.dot(G.T, M1), ZERO, ZERO, -mu0*c**2*M0], [ZERO, G, ZERO, ZERO, ZERO, ZERO], [-G, ZERO, ZERO, ZERO, ZERO, ZERO], [eps0*wpe**2*np.identity(Nbase_0), ZERO, ZERO, ZERO, ZERO, wce*np.identity(Nbase_0)], [ZERO, eps0*wpe**2*np.identity(Nbase_0), ZERO, ZERO, -wce*np.identity(Nbase_0), ZERO]])

LHS = sc.sparse.csc_matrix(A1 - 1/2*dt*A2)
RHS = sc.sparse.csc_matrix(A1 + 1/2*dt*A2)

LU = sc.sparse.linalg.splu(LHS)

print('LU factorization done!')
#====================================================================================

#===== create particles (z,vx,vy,vz,wk) and sample according to sampling distribution
particles = np.zeros((Np, 5), order='F')
particles[:, 0] = np.random.rand(Np)*Lz
particles[:, 1] = np.random.randn(Np)*wperp
particles[:, 2] = np.random.randn(Np)*wperp
particles[:, 3] = np.random.randn(Np)*wpar

jh = np.zeros(2*Nbase_0)
Fh = np.zeros(6*Nbase_0)
#====================================================================================

#===== parameters for control variate ===============================================
g0 = g_sampling(particles[:, 1], particles[:, 2], particles[:, 3])
w0 = fh0(particles[:, 0], particles[:, 1], particles[:, 2], particles[:, 3])/g_sampling(particles[:, 1], particles[:, 2], particles[:, 3])
#====================================================================================

#===== initialize velocities by pushing back by -dt/2 and compute weights ===========
timea = time.time()

z_old[:] = particles[:, 0]

utils_pic_fast.borisGem_bc_1(particles, -dt/2, qe, me, Lz, Tz, tz, p, Nbase_0, ex, ey, bx, by, B0z)

particles[:, 0] = z_old
particles[:, 4] = w0 - control*maxwell(particles[:, 1], particles[:, 2], particles[:, 3])/g0

timeb = time.time()
print('time for particle push: ' + str(timeb - timea))
#====================================================================================

#===== test timing for hot current computation ======================================
timea = time.time()

utils_pic_fast.hotCurrent_bc_1(particles[:, 1:3], particles[:, 0], particles[:, 4], Tz, p, Nbase_0, qe, jh)

timeb = time.time()
print('time for hot current computation: ' + str(timeb - timea))
#====================================================================================

#===== test timing for linear solver ================================================
timea = time.time()

LU.solve(RHS.dot(uj) + dt*Fh)

timeb = time.time()
print('time for solving linear system: ' + str(timeb - timea))
#====================================================================================

#===== time integration by a time step dt ===========================================
def update():

    # ... save old positions
    z_old[:] = particles[:, 0]
    # ...

    # ... update particle velocities from n-1/2 to n+1/2 with fields at time n and positions from n to n+1 with velocities at n+1/2
    utils_pic_fast.borisGem_bc_1(particles, dt, qe, me, Lz, Tz, tz, p, Nbase_0, ex, ey, bx, by, B0z)
    # ...

    # ... update weights with control variate
    particles[:, 4] = w0 - control*maxwell(particles[:, 1], particles[:, 2], particles[:, 3])/g0
    # ...

    # ... compute hot electron current densities
    utils_pic_fast.hotCurrent_bc_1(particles[:, 1:3], 1/2*(z_old + particles[:, 0]), particles[:, 4], Tz, p, Nbase_0, qe, jh)
    # ...

    # ... assemble right-hand side of weak formulation
    Fh[:Nbase_0] = -c**2*mu0*jh[0::2]
    Fh[Nbase_0:2*Nbase_0] = -c**2*mu0*jh[1::2]
    # ...

    # ... time integration of E, B, jc from n to n+1 with Crank-Nicolson method (use hot current density at n+1/2)
    uj[:] = LU.solve(RHS.dot(uj) + dt*Fh)

    ex[:] = uj[:Nbase_0]
    ey[:] = uj[Nbase_0:2*Nbase_0]
    bx[:] = uj[2*Nbase_0:3*Nbase_0]
    by[:] = uj[3*Nbase_0:4*Nbase_0]
    yx[:] = uj[4*Nbase_0:5*Nbase_0]
    yy[:] = uj[5*Nbase_0:]
    # ...
#====================================================================================

#===== create data file and save parameters (first row), initial fields and energies (second row)
if save == 1:
    file = open(title, 'ab')
    np.savetxt(file, np.reshape(pa, (1, 1*Nbase_0 + 5)), fmt = '%1.10e')

en_E = np.append(en_E, eps0/2*(np.dot(ex, np.dot(M0, ex)) + np.dot(ey, np.dot(M0, ey))))
en_B = np.append(en_B, eps0/(2*mu0)*(np.dot(bx, np.dot(M1, bx)) + np.dot(by, np.dot(M1, by))))
en_C = np.append(en_C, 1/(2*eps0*wpe**2)*(np.dot(yx, np.dot(M0, yx)) + np.dot(yy, np.dot(M0, yy))))
en_H = np.append(en_H, me/(2*Np)*np.dot(particles[:, 4], particles[:, 1]**2 + particles[:, 2]**2 + particles[:, 3]**2) + control*Eh_eq)

bx_save = np.append(np.copy(bx), bx[0])

if save == 1:
    data = np.append(bx, np.array([en_E[-1], en_B[-1], en_C[-1], en_H[-1], 0.]))
    np.savetxt(file, np.reshape(data, (1, 1*Nbase_0 + 5)), fmt = '%1.10e')
#====================================================================================

#===== time integration =============================================================
if time_integr == 1:

    print('start time integration! (number of time steps : ' + str(int(T/dt)) + ')')
    time_step = 0

    while True:

        try:
            if time_step*dt >= T:
                if save == 1:
                    file.close()
                break

            if time_step%50 == 0:
                print('time steps finished: ' + str(time_step))

            update()

            if time_step%saving_step == 0:

                # ... add data to file
                en_E = np.append(en_E, eps0/2*(np.dot(ex, np.dot(M0, ex)) + np.dot(ey, np.dot(M0, ey))))
                en_B = np.append(en_B, eps0/(2*mu0)*(np.dot(bx, np.dot(M1, bx)) + np.dot(by, np.dot(M1, by))))
                en_C = np.append(en_C, 1/(2*eps0*wpe**2)*(np.dot(yx, np.dot(M0, yx)) + np.dot(yy, np.dot(M0, yy))))
                en_H = np.append(en_H, me/(2*Np)*np.dot(particles[:, 4], particles[:, 1]**2 + particles[:, 2]**2 + particles[:, 3]**2) + control*Eh_eq)

                bx_save = np.vstack((bx_save, np.append(np.copy(bx), bx[0])))

                if save == 1:
                    data = np.append(bx, np.array([en_E[-1], en_B[-1], en_C[-1], en_H[-1], (time_step + 1)*dt]))
                    np.savetxt(file, np.reshape(data, (1, 1*Nbase_0 + 5)), fmt = '%1.10e')
                # ...

            time_step += 1
        except KeyboardInterrupt:
            print('Pausing...  (Hit ENTER to continue, type quit to exit.)')

            if save == 1:
                file.close()

            try:
                response = input()
                if response == 'quit':
                    break
                print('Resuming...')

                if save == 1:
                    file = open(title, 'ab')

            except KeyboardInterrupt:
                print('Resuming...')

                if save == 1:
                    file = open(title, 'ab')

                continue

    if save == 1:
        file.close()
#====================================================================================
```

    pyccelization of pic functions done!
    matrix assembly done!
    projection of initial fields done!
    LU factorization done!
    time for particle push: 0.04896354675292969
    time for hot current computation: 0.01453852653503418
    time for solving linear system: 0.02163839340209961
    start time integration! (number of time steps : 4000)
    time steps finished: 0
    time steps finished: 50
    time steps finished: 100
    time steps finished: 150
    time steps finished: 200
    time steps finished: 250
    time steps finished: 300
    time steps finished: 350
    time steps finished: 400
    time steps finished: 450
    time steps finished: 500
    time steps finished: 550
    time steps finished: 600
    time steps finished: 650
    time steps finished: 700
    time steps finished: 750
    time steps finished: 800
    time steps finished: 850
    time steps finished: 900
    time steps finished: 950
    time steps finished: 1000
    time steps finished: 1050
    time steps finished: 1100
    time steps finished: 1150
    time steps finished: 1200
    time steps finished: 1250
    time steps finished: 1300
    time steps finished: 1350
    time steps finished: 1400
    time steps finished: 1450
    time steps finished: 1500
    time steps finished: 1550
    time steps finished: 1600
    time steps finished: 1650
    time steps finished: 1700
    time steps finished: 1750
    time steps finished: 1800
    time steps finished: 1850
    time steps finished: 1900
    time steps finished: 1950
    time steps finished: 2000
    time steps finished: 2050
    time steps finished: 2100
    time steps finished: 2150
    time steps finished: 2200
    time steps finished: 2250
    time steps finished: 2300
    time steps finished: 2350
    time steps finished: 2400
    time steps finished: 2450
    time steps finished: 2500
    time steps finished: 2550
    time steps finished: 2600
    time steps finished: 2650
    time steps finished: 2700
    time steps finished: 2750
    time steps finished: 2800
    time steps finished: 2850
    time steps finished: 2900
    time steps finished: 2950
    time steps finished: 3000
    time steps finished: 3050
    time steps finished: 3100
    time steps finished: 3150
    time steps finished: 3200
    time steps finished: 3250
    time steps finished: 3300
    time steps finished: 3350
    time steps finished: 3400
    time steps finished: 3450
    time steps finished: 3500
    time steps finished: 3550
    time steps finished: 3600
    time steps finished: 3650
    time steps finished: 3700
    time steps finished: 3750
    time steps finished: 3800
    time steps finished: 3850
    time steps finished: 3900
    time steps finished: 3950

```python
tn = np.linspace(0., T, len(en_B))
plt.semilogy(tn, en_B)

omega = utils_opt.solveDispersionHybrid(k, +1, c, wce, wpe, wpar, wperp, nuh, 0.5 + 0j, 1e-8)[0]
omega_r = np.real(omega)
gamma = np.imag(omega)

t2 = np.linspace(0, 150, 100)
plt.semilogy(t2, 1.8e-9*np.exp(gamma*2*t2), 'k--', label = 'expected growth')
```

```python
from matplotlib.colors import LogNorm
from matplotlib.patches import ConnectionPatch

Nt = len(en_B) - 1

f3 = plt.figure()
f3.set_figheight(5)
f3.set_figwidth(12)

fontsize = 24
#plt.rc('text', usetex = False)
#plt.rc('font', family = 'serif')
plt.rcParams.update({'font.size': fontsize})
plt.rc('xtick', labelsize = fontsize)
plt.rc('ytick', labelsize = fontsize)

w = np.linspace(0, Nt, Nt + 1) - Nt/2
w = 2*np.pi/T*w

ks = np.linspace(0, Nel, Nel + 1) - Nel/2
ks = 2*np.pi/Lz*ks

Bxkw = np.fft.fft2(bx_save)
K, W = np.meshgrid(ks, w)
Bkw = np.fft.fftshift(Bxkw)
Bkw_plot = np.abs(Bkw)

ax1 = plt.subplot(121)

ticks = np.power(10, np.linspace(-8, 0, 5))
lvls = np.logspace(-8, 0, 60)
plt.contourf(K, W, Bkw_plot/Bkw_plot.max(), cmap = 'jet', norm = LogNorm(), levels = lvls)
plt.plot(np.linspace(0, 8, 10), np.zeros(10), 'k')
plt.plot(np.linspace(0, 8, 10), np.ones(10)*6, 'k')
plt.plot(np.zeros(10), np.linspace(0, 6, 10), 'k')
plt.plot(np.ones(10)*8, np.linspace(0, 6, 10), 'k')
plt.ylim((-20, 20))
plt.xlabel(r'$kc/ |\Omega_\mathrm{ce}|$')
plt.ylabel(r'$\omega_\mathrm{r}/ |\Omega_\mathrm{ce}|$')
plt.title(r'$|\hat{B}_x(k,\omega_\mathrm{r})|/|\hat{B}_x(k,\omega_\mathrm{r})|_\mathrm{max}$', fontsize = fontsize)

# ... analytical dispersion relation
k1 = np.linspace(0.1, 8.0, 40)

w1_1 = np.zeros(40)
w1_2 = np.zeros(40)
w1_3 = np.zeros(40)

w1_1[0] = 0.0001
w1_2[0] = 1.5001
w1_3[0] = 2.5001

for i in range(0, 40):

    if i == 0:
        w1_1[i] = utils_opt.solveDispersionCold(k1[i], +1, c, wce, wpe, w1_1[i], 1e-6, 100)[0]
        w1_2[i] = utils_opt.solveDispersionCold(k1[i], -1, c, wce, wpe, w1_2[i], 1e-6, 100)[0]
        w1_3[i] = utils_opt.solveDispersionCold(k1[i], +1, c, wce, wpe, w1_3[i], 1e-6, 100)[0]

    else:
        w1_1[i] = utils_opt.solveDispersionCold(k1[i], +1, c, wce, wpe, w1_1[i-1], 1e-6, 100)[0]
        w1_2[i] = utils_opt.solveDispersionCold(k1[i], -1, c, wce, wpe, w1_2[i-1], 1e-6, 100)[0]
        w1_3[i] = utils_opt.solveDispersionCold(k1[i], +1, c, wce, wpe, w1_3[i-1], 1e-6, 100)[0]
# ...

ax2 = plt.subplot(122)

ticks = np.power(10, np.linspace(-8, 0, 5))
lvls = np.logspace(-8, 0, 60)
plt.contourf(K, W, Bkw_plot/Bkw_plot.max(), cmap = 'jet', norm = LogNorm(), levels = lvls)
plt.xlim((0, 8))
plt.ylim((0, 6))
plt.xticks([], [])
plt.yticks([], [])
plt.plot(k1, w1_1, 'k--')
plt.plot(k1, w1_2, 'k--')
plt.plot(k1, w1_3, 'k--')
plt.subplots_adjust(hspace = 0.3)
plt.tight_layout()

con1 = ConnectionPatch(xyA = (0, 6), xyB = (0, 6), coordsA = "data", coordsB = "data", axesA = ax2, axesB = ax1, color = "black", linewidth = 1)
con2 = ConnectionPatch(xyA = (0, 0), xyB = (0, 0), coordsA = "data", coordsB = "data", axesA = ax2, axesB = ax1, color = "black", linewidth = 1)
con3 = ConnectionPatch(xyA = (0, 4.3), xyB = (8, 6), coordsA = "data", coordsB = "data", axesA = ax2, axesB = ax1, color = "black", linewidth = 1)
con4 = ConnectionPatch(xyA = (0, 2.4), xyB = (8, 0), coordsA = "data", coordsB = "data", axesA = ax2, axesB = ax1, color = "black", linewidth = 1)

ax2.add_artist(con1)
ax2.add_artist(con2)
ax2.add_artist(con3)
ax2.add_artist(con4)

#plt.title('B-splines: Strang', fontsize = fontsize)
#plt.savefig('Bsplines_Strang_spectrum.png', bbox_inches = 'tight')
```

```python

```


Diodes
===

The incident flux and the current that is generated by a photodiode subjected to it are related by

$$
\begin{equation}
\begin{split}
I(A)=&\sum_{i,j}P_{i,j}(W)R_{j}(A/W)+D(A)\\
P_{i,j}(W)=&I_{i,j}(Hz)E_{j}(\text{keV})\\
R_{j}(A/W)=&\frac{e(C)}{E_{h}(\text{keV})}[1-e^{-\mu(E_{j})\rho d}]
\end{split}
\end{equation}
$$

where P the incident power, R the spectral responsivity, D the dark current, $E_i$ the energy of the incident photon, $E_j$ the energy of the detected photon, $E_{h}$ the energy to create an electron-hole pair, $I_{i,j}$ the detected flux of line $j$ due to line $i$ and diode density $\rho$, mass attenuation coefficient $\mu$ and thickness $d$.

The relationship between the detected flux and the flux at the sample position is given by

$$
\begin{equation}
I_{i,j}(Hz)=I_{0}(Hz) w_i Y_{i,j} = I_{s}(Hz)\frac{w_i Y_{i,j}}{\sum_k w_k T_{s}(E_{k})}
\end{equation}
$$

with the following factors
* $I_0$: total flux before detection
* $I_s$: the total flux seen by the sample
* $T_s$: the "transmission" between source and sample (product of several transmission factors and optics efficiency)
* $w_k$: the fraction of primary photons with energy $E_{k}$
* $Y_{i,j}$: the "rate" of detected line $j$ due to source line $i$ (not including detector attenuation)

The line fractions at the sample position are

$$
\begin{equation}
\begin{split}
I_{i,s}=& I_0 w_i T_{s}(E_{i})\\
w_{i,s} =& \frac{I_{i,s}}{\sum_k I_{k,s}} = \frac{w_i T_{s}(E_{i})}{\sum_k w_k T_{s}(E_{k})}
\end{split}
\end{equation}
$$

The relationship between the flux reaching the sample and the current measured by a pn-diode can be summarized as

$$
\begin{equation}
\begin{split}
I(A)=&I_{s}(Hz)C_s(C)+D(A)\\
C_s(C) =& \frac{\sum_{i,j} w_i Y_{i,j}C_j}{\sum_k w_k T_{s}(E_{k})}\\
C_j(C) =& E_{j}(\text{keV})\frac{e(C)}{E_{h}(\text{keV})}[1-e^{-\mu(E_{j})\rho d}]\\
\end{split}
\end{equation}
$$

where $C_s$ the charge generated per photon reaching the sample and $C_j$ the charge generated per photon reaching the diode. A simplified relationship with a lookup table can be used

$$
\begin{equation}
C_s(C) = \sum_i w_i \mathrm{LUT}(E_i)
\end{equation}
$$

Finally in order to allow a fast read-out, current is converted to frequency by an oscillator

$$
\begin{equation}
I(\text{Hz})=\frac{F_{\text{max}}(Hz)}{V_{\text{max}}(V)}
\frac{V_{\text{max}}(V)}{I_{\text{max}}(A)}I(A)+F_{0}(Hz)
\end{equation}
$$

where $F_{\text{max}}$ the maximal frequency that can be detected, $F_{0}$ a fixed offset, $V_{\text{max}}$ the maximal output voltage of the ammeter and input voltage of the oscillator, $\frac{V_{\text{max}}(V)}{I_{\text{max}}(A)}$ the "gain" of the ammeter. Sometimes $I_{\text{max}}(A)$ is referred to as the diode "gain".

Absolute diode
--------------
An absolute diode has a spectral responsivity $R(A/W)$ which behaves as theoretically expected

```python
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from spectrocrunch.detectors import diode
det = diode.factory("sxm_ptb")
print(det)

det.model = False
energy = det.menergy
R = det.spectral_responsivity(energy)
plt.plot(energy,R,marker='o',linestyle="",label='measured')

det.model = True
energy = np.linspace(1,10,100)
R = det.spectral_responsivity(energy)
plt.plot(energy,R,label='model')

plt.legend()
plt.xlabel('Energy (keV)')
plt.ylabel('Spectral responsivity (A/W)')
plt.show()
```

Calibrated diode
----------------
The spectral responsivity $R(A/W)$ of a calibrated diode is determined by the difference in response with an absolute diode

```python
det = diode.factory("sxm_idet",npop=4)
print(det)

det.model = False
energy = det.menergy
R = det.spectral_responsivity(energy)
plt.plot(energy,R,marker='o',linestyle="",label='measured')

det.model = True
energy = np.linspace(1,12,100)
R = det.spectral_responsivity(energy)
plt.plot(energy,R,label='model')

det.model = False
R = det.spectral_responsivity(energy)
plt.plot(energy,R,label='used')

plt.legend(loc='best')
plt.xlabel('Energy (keV)')
plt.ylabel('Spectral responsivity (A/W)')
plt.show()
```

Direct detection
------------------

When $Y_{i,j}$ only contains transmission factors (i.e. not fluorescence/scattering from a secondary target) the diode measures $I_s$ directly. The relationship between flux $I_s(Hz)$ and diode response $I(Hz)$ is determined by the spectral responsivity which should be known (absolute diode) or calibrated (with respect to an absolute diode):

```python
from spectrocrunch.optics import xray as xrayoptics

atmpath = xrayoptics.Filter(material="vacuum", thickness=1)
det = diode.factory("sxm_idet",optics=[atmpath])
print(det)

Is = 3e9
energy = np.linspace(2,10,100)

for gain in [1e7]:
    for model in [False,True]:
        det.gain = gain
        det.model = model
        I = det.fluxtocps(energy[:, np.newaxis], Is).to("Hz").magnitude
        plt.plot(energy,I,label="{:.0e} V/A{}".\
                 format(gain," (model)" if model else ""))

plt.gca().axhline(y=det.oscillator.Fmax.to("Hz").magnitude,label="Fmax",\
                  color='k',linestyle='--')

plt.title("Flux = {:.0e} Hz".format(Is))
plt.legend(loc='best')
plt.xlabel('Energy (keV)')
plt.ylabel('Response (Hz)')
plt.show()
```

Indirect detection
------------------

The conversion factors $Y_{i,j}$ can be calculated from the cross-sections of the secondary target.

### Without optics
When the indirect diode measures the flux downstream from the optics (or there are no optics at all), the relationship between flux and measured count-rate is known (because $T_s$ is known):

```python
atmpath = xrayoptics.Filter(material="vacuum", thickness=1)
iodet = diode.factory("sxm_iodet1",optics=[atmpath])
iodet.gain = 1e9
print(iodet.geometry)

Is = 5e9
energy = np.linspace(1,10,50)
I = iodet.fluxtocps(energy[:, np.newaxis], Is).to("Hz").magnitude
plt.plot(energy,I)

plt.gca().axhline(y=iodet.oscillator.Fmax.to("Hz").magnitude,label="Fmax",\
                  color='k',linestyle='--')

plt.title("Flux = {:.0e} Hz".format(Is))
plt.xlabel('Energy (keV)')
plt.ylabel('Response (Hz)')
plt.show()
```

### With optics

In case the indirect diode is upstream from the optics, transmission $T_s$ needs to be calibrated with a direct diode. This is done by measuring a changing flux at fixed energy, e.g. by scanning a series of attenuators. The flux is calculated from the direct diode and used to calibrate the response of the indirect diode:

```python
iodet = diode.factory("sxm_iodet1",optics=[atmpath, "KB"])
iodet.gain = 1e8

idet = diode.factory("sxm_idet")
idet.gain = 1e6

energy = 7
idetresp = np.linspace(3e4,5e4,100)
fluxmeas = idet.cpstoflux(energy,np.random.poisson(idetresp))

iodetresp = np.random.poisson(np.linspace(2e5,3e5,100))
fitinfo = iodet.calibrate(iodetresp,fluxmeas,energy,caliboption="optics")
print(iodet.geometry)

plt.plot(fluxmeas,iodetresp,marker='o',linestyle="")
plt.plot(fluxmeas,iodet.fluxtocps(energy,fluxmeas))
label = "\n".join(["{} = {}".format(k,v) for k,v in fitinfo.items()])
plt.annotate(label,xy=(0.5,0.1),xytext=(0.5,0.1),\
            xycoords="axes fraction",textcoords="axes fraction")

plt.title("Gain = {:~.0e}".format(iodet.gain))
plt.xlabel('Flux (Hz)')
plt.ylabel('Response (Hz)')
plt.show()
```

Note that the slope is $C_s(C)$ (the charge generated per photon reaching the sample, expressed here in units of elementary charge) and the intercept $D(A)$ (the dark current of the diode).

### Manual calibration

Calibration can also be done manually for a single flux-reponse pair. The response is expected to be $I(Hz)$ but it can also be $I(A)$. If you want a linear energy interpolation, calibration can also be simplified in which case it simply stores a lookup table for $C_s(C)$.

```python
#Specify quantities manually with units:
#from spectrocrunch.patch.pint import ureg
#current = ureg.Quantity(1e-8,"A")

for simple in [True, False]:
    iodet = diode.factory("sxm_iodet1",optics=[atmpath, "KB"],simplecalibration=simple)
    iodet.gain = 1e8

    # Calibrate with Hz-Hz pair
    cps = 100000
    flux = 1e9
    energy = 6

    iodet.calibrate(cps,flux,energy,caliboption="optics")
    current = iodet.fluxtocurrent(energy,flux)

    # Calibrate with A-Hz pair
    energy = 10
    current *= 0.5
    iodet.calibrate(current,flux,energy,caliboption="optics")
    label = "C$_s$ table" if simple else "Calibrated T$_s$"
    print(label)
    print(iodet)
    print("")

    energy = np.linspace(6,10,10)
    response = [iodet.fluxtocps(en,flux).magnitude for en in energy]
    plt.plot(energy,response,label=label)

plt.legend()
plt.title("Gain = {:~.0e}, Flux = {:.0e}".format(iodet.Rout,flux))
plt.xlabel('Energy (keV)')
plt.ylabel('Response (Hz)')

plt.show()
```


# Matrices

Olagoke Oladokun
Covenant University, Ota, Nigeria

©2021

```python
!pip install -q numpy
!pip install -q scipy
!pip install -q sympy
```

    WARNING: You are using pip version 21.1.3; however, version 21.2.1 is available.
    You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
    WARNING: You are using pip version 21.1.3; however, version 21.2.1 is available.
    You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
    WARNING: You are using pip version 21.1.3; however, version 21.2.1 is available.
    You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.

## GEC220
## Engineering Mathematics II

Matrices and Determinants: Solution of system of linear equations by determinants. Linear dependence and independence, rank of a matrix. General system of linear equations, existence and properties of solution, Gaussian elimination. Matrix inverse by elementary matrices, adjoint, and partitioning methods. Characteristic polynomial, characteristic equation, eigenvalues and eigenvectors.

GEC220
Engineering Mathematics II
3 Units
LH 45
Lecturers
	Prof. Vincent
	Dr. Adeeyo
	Dr. Olagoke

Partial Differentiation: Functions of several variables, continuity and partial derivatives. Total differentials, approximate calculations using differentials. Chain rule. Implicit differentiation. Series representation of functions (Maclaurin & Taylor’s), Taylor’s Theorem. Extremum problems, (analytic method) without and with constraints, Lagrange multipliers, global extremum. Ordinary Differential Equations: Definition, degree, order, linear, non-linear, solution. First order equations, separable variables, equations reducible to separable form, exact equations, integrating factors, homogenous differential equations. Modeling of engineering systems leading to first order differential equations- electric circuit, mixing/dilution, radioactive decay, bacterial culture. 2nd order differential equations with constant coefficients, homogeneous, non-homogeneous, complementary functions, particular integrals, D-operator method. General linear second-order differential equations (without using matrices). Power series solution, Legendre’s differential equation. Modeling of engineering systems leading to 2nd order differential equations- electric circuit, mechanical oscillations-free and forced, resonance. Pre-requisites: MAT 121 and MAT 122.
Matrices and Determinants: Solution of system of linear equations by determinants. Linear dependence and independence, rank of a matrix. General system of linear equations, existence and properties of solution, Gaussian elimination. Matrix inverse by elementary matrices, adjoint, and partitioning methods. Characteristic polynomial, characteristic equation, eigenvalues and eigenvectors.

Contents
1	Matrices	6
1.1	Matrix notation	7
1.2	Addition and Subtraction of Matrices	7
1.3	Multiplication of Matrices	8
1.3.1	Scalar Multiplication	8
1.3.2	Two matrices multiplication	8
1.4	Transpose of matrix	10
1.5	Square matrix	10
1.5.1	Symmetry matrix	10
1.6	Triangular matrix	11
1.7	Diagonal matrix	11
1.8	Unit matrix	11
1.9	Null matrix	12
2	Determinants	13
2.1	Determinants of the second order	13
2.2	Determinants of the third order	14
3	Solution of system of linear equations by determinants	17
3.1	Determinant Equation Method	18
3.1.1	Secondary Degree	18
3.1.2	Third Degree	19
3.2	Cramers Rule	22
3.2.1	For second degree	22
3.2.2	Third degree	22
4	Linear dependence and independence	25
5	Rank of a matrix	27
6	General system of linear equations,	28
7	Existence and properties of solution	31
7.1	Existence of solution	31
7.2	Other properties of the solution	31
8	Gaussian elimination.	33
8.1	Algorithm of Gaussian Elimination	33
8.2	Determinant from Gaussian Elimination	38
9	Matrix inverse by elementary matrices, adjoint, and partitioning methods.	39
9.1	Inverse of a square matrix	39
9.2	Cofactors	39
9.3	Adjoint	41
10	Eigenvalues and Eigenvectors.	43
10.1	Eigenvalues	43
10.1.1	Example	43
10.2	Eigenvectors	44
10.2.1	Example	44

# Matrices

A matrix is a set of real or complex numbers (or elements) arranged in rows and columns to form a rectangular array.
A matrix can be identified by its order denoted as $m \times n \quad (m \ by \ n)$ matrix.

$$
\left[\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right]
$$

What is the order of matrix above?. Matrix  has order $3\times 3$. The number of rows of A, $m = 3$ and number of column, $n = 3$

```python
import numpy as np

A = np.array([
    [3,-3,-4],
    [1,-1,-1],
    [2,-1,4]
])

print('===============================')
print('matrix A =')
print(A)
print()
print('mxn =', np.shape(A))
print('===============================')
```

    ===============================
    matrix A =
    [[ 3 -3 -4]
     [ 1 -1 -1]
     [ 2 -1  4]]

    mxn = (3, 3)
    ===============================

Similarly, the matrix $C$ below is of order $4\times2$

$$
\left[\begin{matrix}\begin{matrix}1&2\\\end{matrix}\\\begin{matrix}4&3\\-1&-5\\3&3\\\end{matrix}\\\end{matrix}\right]=m\times n=4\times2
$$
$$
C=\left[\begin{matrix}\begin{matrix}1&2\\\end{matrix}\\\begin{matrix}4&3\\-1&-5\\3&3\\\end{matrix}\\\end{matrix}\right]=m\times n=4\times2
$$

```python
import numpy as np

C = np.array([
    [1,2], [4,3],[-1,-5], [3,3],
])

print('===============================')
print('matrix C =\n', C)
print()
print('mxn =', np.shape(C))
print('===============================')

```

    ===============================
    matrix C =
     [[ 1  2]
     [ 4  3]
     [-1 -5]
     [ 3  3]]

    mxn = (4, 2)
    ===============================

Exercises

What are the order of the matrices below:
$$
\left[
    \begin{matrix}
        1 & -2 & -4
    \end{matrix}
\right]
$$
(A) $2 \times 2$ (B) $1 \times -3$ (C) $3 \times 1$ (D) $1 \times 3$

## Matrix notation

$$
A=\left[
    \begin{matrix}
        a_{11}&a_{12}&a_{13}\\
        a_{21}&a_{22}&a_{23}\\
        a_{31}&a_{32}&a_{33}\\
    \end{matrix}
    \right]
$$
Matrix $A$ is of order $3 \times 3$ with elements $a_{ij}$ where $i$ and $j$ represents the index of the row and column it belongs in the matrix.

## Addition and Subtraction of Matrices

Addition and subtraction of two matrices can only be achieved if they are the same order.
$$
A\pm B
$$

Only possible if
$$
\left.A\right|m{==}\left.B\right|m\ \\ and \\  \left.A\right|n{==}\left.B\right|n
$$

Example

$$
A=\left[\begin{matrix}1&-3\\4&-2\\\end{matrix}\right]
$$
$$
B=\left[\begin{matrix}2&-3\\4.1&-4.5\\\end{matrix}\right]
$$
Then
Check the order of $A (2 \times 2)$ and compare to order $B (2 \times 2)$?
Since they are both equal the addition and subtraction is possible.

$$
\left[\begin{matrix}1&-3\\4&-2\\\end{matrix}\right]+\left[\begin{matrix}2&-3\\4.1&-4.5\\\end{matrix}\right]=\left[\begin{matrix}3&-6\\8.1&-6.5\\\end{matrix}\right]
$$
$$
\left[\begin{matrix}1&-3\\4&-2\\\end{matrix}\right]-\left[\begin{matrix}2&-3\\4.1&-4.5\\\end{matrix}\right]=\left[\begin{matrix}-1&0\\-0.1&2.5\\\end{matrix}\right]
$$

```python
import numpy as np

A = np.array([[1,-3],[4,-2]])
B = np.array([[2,-3],[4.1,-4.5]])

sum = A + B
diff = A - B

print('============================')

print('sum = \n', sum)
print()
print('diff = \n', diff)

print('============================')

```

    ============================
    sum =
     [[ 3.  -6. ]
     [ 8.1 -6.5]]

    diff =
     [[-1.   0. ]
     [-0.1  2.5]]
    ============================

## Multiplication of Matrices
### Scalar Multiplication

$$
4\times A= 4\times\left[
    \begin{matrix}
    1&-3 \\
    4&-2 \\
    \end{matrix}
    \right]
    =
    \left[
        \begin{matrix}
        4&-12\\
        16&-8\\
        \end{matrix}
    \right]
$$

```python
import numpy as np

A = np.array(
    [
        [1, -3],
        [4, -2]
    ])

prod = 4*A

print('============================')
print('A= ')
print(A)
print('4*A =\n', prod)
print('============================')

```

    ============================
    A=
    [[ 1 -3]
     [ 4 -2]]
    4*A =
     [[  4 -12]
     [ 16  -8]]
    ============================

## Two matrices multiplication
To multiply any two matrices the number of columns in the first matrix must be equal to the number of rows of the second matrix. The product matrix will have number of rows of the first matrix and number of columns of the second matrix
$$
A\times B =P
$$
Is possible if and only if
$$
    A|n == B|m \quad \Rightarrow \quad P=A|m\times B|n
$$

Example
$$
C=\left[\begin{matrix}\begin{matrix}1&2\\\end{matrix}\\\begin{matrix}4&3\\-1&-5\\3&3\\\end{matrix}\\\end{matrix}\right]=m\times n=4\times2
$$
$$
A=\left[\begin{matrix}1&-3\\4&-2\\\end{matrix}\right]=m\times n=2\times2
$$

$$
A\times C \\
(2\times2)\ \times(4\times2) \\
2\times(2\neq4)\times2
$$

$$
(2\neq4)∶\ A\times C \textrm{ is not possible.}
$$

```python
import numpy as np

C = np.array(
    [
        [1,2],
        [4,3],
        [-1,-5],
        [3,3],
    ]
)

A = np.array(
    [
        [1,-3],
        [4,-2]
    ]
)

P = A @ C
```

Interchanging
$$
C\times A \\
(4\times2)\ \times(2\times2) \\
4\times(2=2)\times2 \\
$$
$$
(2=4)∶\ C\times A \textrm{ is possible.} \\
C\times A\equiv4\times2
$$

Let’s carryout the multiplication.
$$
C\times A=\left[\begin{matrix}\begin{matrix}c_{11}&c_{12}\\\end{matrix}\\\begin{matrix}c_{21}&c_{22}\\c_{31}&c_{32}\\c_{41}&c_{42}\\\end{matrix}\\\end{matrix}\right]\times\left[\begin{matrix}a_{11}&a_{12}\\a_{21}&a_{22}\\\end{matrix}\right]
$$

$$
C\times A=\left[\begin{matrix}\begin{matrix}c_{11}a_{11}+c_{12}a_{21}&c_{11}a_{12}+c_{12}a_{22}\\\end{matrix}\\\begin{matrix}c_{21}a_{11}+c_{22}a_{21}&c_{21}a_{12}+c_{22}a_{22}\\c_{31}a_{11}+c_{32}a_{21}&c_{31}a_{12}+c_{32}a_{22}\\c_{41}a_{11}+c_{42}a_{21}&c_{41}a_{12}+c_{42}a_{22}\\\end{matrix}\\\end{matrix}\right]
$$

$$
C\times A=\left[\begin{matrix}\begin{matrix}1\times1+2\times4\ \ \ \ \ \ &1\times-3+2\times-2\\\end{matrix}\\\begin{matrix}4\times1+3\times4&4\times-3+3\times-2\\-1\times1+-5\times4&-1\times-3+-5\times-2\\3\times1+3\times4&3\times-3+3\times-2\\\end{matrix}\\\end{matrix}\right]=\left[\begin{matrix}\begin{matrix}9&-7\\\end{matrix}\\\begin{matrix}16&-18\\-21&13\\15&-15\\\end{matrix}\\\end{matrix}\right]
$$

```python
import numpy as np

C = np.array(
    [
        [1,2],
        [4,3],
        [-1,-5],
        [3,3],
    ]
)

A = np.array(
    [
        [1,-3],
        [4,-2]
    ]
)

P = C @ A

print('=========================')
print('P=\n', P)
print('=========================')

```

    =========================
    P=
     [[  9  -7]
     [ 16 -18]
     [-21  13]
     [ 15 -15]]
    =========================

## Transpose of matrix
Interchanging the rows and columns of a matrix is called transpose.
$$
A=\left[\begin{matrix}1&-3\\4&-2\\\end{matrix}\right]
$$
$$
A^T=\left[\begin{matrix}1&4\\-3&-2\\\end{matrix}\right]
$$

```python
import numpy as np

C = np.array(
    [
        [1,2],
        [4,3],
        [-1,-5],
        [3,3],
    ]
)

A = np.array(
    [
        [1,-3],
        [4,-2]
    ]
)

Atrp = np.transpose(A)

print('=========================')
print('A=\n', A)
print('A^T=\n', Atrp)
print()
print('C=\n', C)
print('C^T=\n', np.transpose(C))
print('=========================')

```

    =========================
    A=
     [[ 1 -3]
     [ 4 -2]]
    A^T=
     [[ 1  4]
     [-3 -2]]

    C=
     [[ 1  2]
     [ 4  3]
     [-1 -5]
     [ 3  3]]
    C^T=
     [[ 1  4 -1  3]
     [ 2  3 -5  3]]
    =========================

### Exercise 2
$$
A=\left(\begin{matrix}1&-1&-1\\0&-2&3\\2&1&5\\\end{matrix}\right),\ B=\left(\begin{matrix}1&1&1\\1&1&1\\\end{matrix}\right)
$$

1. $A \times B$
2. $B \times A$
3. $A \times B^T$
4. $B \times A^T$

```python
import numpy as np

A = np.array(
    [
        [1, -1, -1],
        [0, -2, 3],
        [2, 1, 5],
    ]
)

B = np.array(
    [
        [1, 1, 1],
        [1, 1, 1],
    ]
)

tr y:
    print('A*B=\n', A * B)
except ValueError:
    print('Error multiplying A*B')

try:
    print('B*A=\n', B @ A)
except ValueError:
    print('Error multiplying B*A')

try:
    print('A*B^T=\n', A @ np.transpose(B))
except ValueError:
    print('Error multiplying A*B^T')

try:
    print('B*A^T=\n', B @ np.transpose(A))
except ValueError:
    print('Error multiplying B*A^T=')

```

    Error multiplying A*B
    B*A=
     [[ 3 -2  7]
     [ 3 -2  7]]
    A*B^T=
     [[-1 -1]
     [ 1  1]
     [ 8  8]]
    B*A^T=
     [[-1  1  8]
     [-1  1  8]]

### Exercise 3

$$
A=\left(\begin{matrix}1&-1&-1\\0&-2&3\\2&1&5\\\end{matrix}\right)
$$

Find the determinant of A:

```python
import numpy as np

A = np.array(
    [
        [1, -1, -1, 0],
        [0, -2, 3, 2],
        [2, 1, 5, 1],
        [-2, 1, -2, 9],
    ]
)

np.linalg.det(A)
```

    -227.99999999999986

### Exercise 4
$$
A . x = b \\
$$
$$
A=  \left(
        \begin{matrix}
            1 & -1 & -1 & 0\\
            0 & -2 & 3 & 2\\
            2 & 1 & 5 & 1\\
            -2 & 1 & -2 & 9\
        \end{matrix}
    \right) \\
b=  \left(
        \begin{matrix}
            4\\
            4\\
            4\\
            4\\
        \end{matrix}
    \right)
$$

```python
import numpy as np

A = np.array(
    [
        [1, -1, -1, 0],
        [0, -2, 3, 2],
        [2, 1, 5, 1],
        [-2, 1, -2, 9],
    ]
)

b = np.array(
    [
        [4],
        [4],
        [4],
        [4],
    ]
)

x = np.linalg.solve(A,b)

print('x = \n', x)

```

    x =
     [[ 2.57894737]
     [-1.21052632]
     [-0.21052632]
     [ 1.10526316]]

```python
from sympy import *

a, b, c, d = symbols('a b c d')

linsolve(Matrix((
        [1, -1, -1, 0, 4],
        [0, -2, 3, 2, 4],
        [2, 1, 5, 1, 4],
        [-2, 1, -2, 9, 4])), (a,b,c,d))
```

$\displaystyle \left\{\left( \frac{49}{19}, \  - \frac{23}{19}, \  - \frac{4}{19}, \  \frac{21}{19}\right)\right\}$

### Exercise 5
Solve for $x, y, z$ in the augment matrix $M$.

$$
M =  \left(
        \begin{matrix}
            8 & 4 & -1 & 3\\
            1 & 6 & 2 & 3\\
            4 & 0 & 2 & 3\\
        \end{matrix}
    \right)
$$

```python
from sympy import *

x,y,z=symbols('x y z')

linsolve(Matrix((
    [8, 4, -1, 3],
    [1, 6, 2, 3],
    [4, 0, 2, 3])), (x, y, z))

```

$\displaystyle \left\{\left( \frac{3}{8}, \  \frac{3}{16}, \  \frac{3}{4}\right)\right\}$

### Exercise 6
Solve for $x, y, z$ in the augment matrix $M$.

$$
M =  \left(
        \begin{matrix}
            3 & -1 & -1 & 4\\
            4 & -2 & 3 & 2\\
            2 & 1 & -5 & -1\\
        \end{matrix}
    \right)
$$

```python
import numpy as np
from fractions import Fraction

M = np.array(
    [
        [3, -1, -1, 4],
        [4, -2, 3, 2],
        [2, 1, -5, -1],
    ],dtype=float
)

print('M =\n', M)
(m,n) = M.shape
print('mxn =\n', m, 'x', n)
print('|A| = \n', M[:3,:3], '\n =', np.linalg.det(M[:3,:3]))

k = 0
p = 0

for p in range(0,m-1):
    for i in range(p+1,m):
        if M[p,p] == 0:
            # change row
            for j in range(i,m):
                if M[j,p] != 0:
                    k += 1
                    Mp = np.copy(M[p])
                    M[p] = M[j]
                    M[j] = Mp

                    print('Mx =\n', M)
                    break

        M[i] = M[i,:] - M[p,:]*M[i,p]/M[p,p]
        print('M =\n', M)

detA = 1
for p in range(m):
    detA *= M[p,p]

print('|A| =', detA)

```

    M =
     [[ 3. -1. -1.  4.]
     [ 4. -2.  3.  2.]
     [ 2.  1. -5. -1.]]
    mxn =
     3 x 4
    |A| =
     [[ 3. -1. -1.]
     [ 4. -2.  3.]
     [ 2.  1. -5.]]
     = -13.0
    M =
     [[ 3.         -1.         -1.          4.        ]
     [ 0.         -0.66666667  4.33333333 -3.33333333]
     [ 2.          1.         -5.         -1.        ]]
    M =
     [[ 3.         -1.         -1.          4.        ]
     [ 0.         -0.66666667  4.33333333 -3.33333333]
     [ 0.          1.66666667 -4.33333333 -3.66666667]]
    M =
     [[  3.          -1.          -1.           4.        ]
     [  0.          -0.66666667   4.33333333  -3.33333333]
     [  0.           0.           6.5        -12.        ]]
    |A| = -12.999999999999995

```python
from sympy import *

x,y,z=symbols('x y z')

linsolve(
    Matrix(
        (
            [3, -1, -1, 4],
            [4, -2, 3, 2],
            [2, 1, -5, -1]
        )
    ),
    (x, y, z)
)

```

$\displaystyle \left\{\left( - \frac{21}{13}, \  -7, \  - \frac{24}{13}\right)\right\}$

```python
from sympy import *
x, y, z=symbols('x y z')

linsolve(Matrix((
    [1, 1, 1, 1],
    [1, 1, 2, 3],
    [1, 0, 2, 3])), (x, y, z))
```

$\displaystyle \left\{\left( -1, \  0, \  2\right)\right\}$

# Test 2 Make Up

Consider the system of linear equation below:

$$
3x\ -y-2-z=\ 4\bigm4z-2y+3x\ +y=2\bigm2x+y-5y-1+z+x\ \ \ =-1
$$

Q1.
	Write the matrices form.	A\bullet x=b
	What is the order of matrix  A
	What is the order of matrix b.
	Determinant of matrix A
	Rank of A
Q2.
	Write the augment matrix A_b,	A_b=\left[A|b\right]
	What is the order of matrix A_b.
	Solve the system of linear equation using Gaussian Elimination.

```python
import numpy as np

A = np.array(
    [
        [3.0, -1., -1.],
        [3., -1., 4.],
        [2., -4., 1.],
    ]
)

b = np.array(
    [
        [6.0],
        [2.0],
        [0.0],
    ]
)

x = np.linalg.solve(A,b)
print('x = \n', x)

d = np.linalg.det(A)
print('det(A) = ', d)

```

    x =
     [[ 2. ]
     [ 0.8]
     [-0.8]]
    det(A) =  50.000000000000014

```python
from sympy import *
x, y, z=symbols('x y z')

linsolve(Matrix((
    [3.0, -1., -1., 6.0],
    [3., -1., 4., 2.0],
    [2., -4., 1., 0],
    )), (x, y, z))
```

$\displaystyle \left\{\left( 2.0, \  0.8, \  -0.8\right)\right\}$

## Square matrix
Square matrix is a matrix of order $m \times m$ or $n \times n$. It contains same number of rows and columns.

$$
A=  \left[
        \begin{matrix}
            a_{11} & a_{12} & \cdots & a_{1n}\\
            a_{21} & a_{22} & \cdots & a_{2n}\\
            \vdots & \vdots & \ddots & \vdots \\
            a_{n1} & a_{n2} & \cdots & a_{nn}\\
        \end{matrix}
    \right]
    =n\times n
    =m\times m
$$
$$
A=\left[\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right]=3\times3
$$

```python
import numpy as np

A = np.array(
    [
        [3,-3,-4],
        [1, -1, -2],
        [2, -1, 4],
    ]
)

Atrp = np.transpose(A)

print('=========================')
print('A=\n', A)
print('\nmxn= ', np.shape(A))
print()
print('A^T=\n', Atrp)
print('\nmxn= ', np.shape(Atrp))
print('=========================')

```

    =========================
    A=
     [[ 3 -3 -4]
     [ 1 -1 -2]
     [ 2 -1  4]]

    mxn=  (3, 3)

    A^T=
     [[ 3  1  2]
     [-3 -1 -1]
     [-4 -2  4]]

    mxn=  (3, 3)
    =========================

### Symmetry matrix
A square matrix is symmetrical if   $a_{ij}=a_{ji}$, meaning $S=S^T$.
$$
S=\left[\begin{matrix}1&2&3\\2&4&5\\3&5&6\\\end{matrix}\right]
$$
$$
S^T=\left[\begin{matrix}1&2&3\\2&4&5\\3&5&6\\\end{matrix}\right]
$$

```python
import numpy as np

A = np.array(
    [
        [3,-3,-4],
        [1, -1, -2],
        [2, -1, 4],
    ]
)

A_T = np.transpose(A)

S = np.array(
    [
        [1, 2, 3],
        [2, 4, 5],
        [3, 5, 6],
    ]
)

S_T = np.transpose(S)
print('=========================')
print('S=\n', S)
print('\nmxn= ', np.shape(S))
print()
print('S^T=\n', S_T)
print('\nmxn= ', np.shape(S_T))
print()
print('S == S^T')
print(S == S_T)

print('=========================')

print('A=\n', A)
print('\nmxn= ', np.shape(A))
print()
print('A^T=\n', A_T)
print('\nmxn= ', np.shape(A_T))
print()
print('A == A^T')
print(A == A_T)
print('=========================')

```

    =========================
    S=
     [[1 2 3]
     [2 4 5]
     [3 5 6]]

    mxn=  (3, 3)

    S^T=
     [[1 2 3]
     [2 4 5]
     [3 5 6]]

    mxn=  (3, 3)

    S == S^T
    [[ True  True  True]
     [ True  True  True]
     [ True  True  True]]
    =========================
    A=
     [[ 3 -3 -4]
     [ 1 -1 -2]
     [ 2 -1  4]]

    mxn=  (3, 3)

    A^T=
     [[ 3  1  2]
     [-3 -1 -1]
     [-4 -2  4]]

    mxn=  (3, 3)

    A == A^T
    [[ True False False]
     [False  True False]
     [False False  True]]
    =========================

### Triangular matrix
A triangular matrix is a square matrix with all elements below the leading diagonal equal to zero.
$$
A=\left[\begin{matrix}1&2&3\\0&4&5\\0&0&6\\\end{matrix}\right]
$$

### Diagonal matrix
A diagonal matrix is a square matrix with all elements zero except the leading diagonal.
$$
A=\left[\begin{matrix}1&0&0\\0&4&0\\0&0&6\\\end{matrix}\right]
$$

### Unit matrix
A Unit matrix is a special diagonal matrix with the leading diagonal element values all unity or ones. The unit matrix is denoted by I.
$$
\mathbb{I}=\left[\begin{matrix}1&0&0\\0&1&0\\0&0&1\\\end{matrix}\right]
$$

#### Useful property
A useful property
$$
A.I=I.A=A
$$

```python
import numpy as np

A = np.array(
    [
        [3,-3,-4],
        [1, -1, -2],
        [2, -1, 4],
    ]
)

I = np.identity(3)

print('=========================')
print('A =\n', A)
print()
print('I =\n', I)
print()
print('A * I=\n', A @ I)
print()
print('I * A=\n', I @ A)
print('=========================')

```

    =========================
    A =
     [[ 3 -3 -4]
     [ 1 -1 -2]
     [ 2 -1  4]]

    I =
     [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]]

    A * I=
     [[ 3. -3. -4.]
     [ 1. -1. -2.]
     [ 2. -1.  4.]]

    I * A=
     [[ 3. -3. -4.]
     [ 1. -1. -2.]
     [ 2. -1.  4.]]
    =========================

## Null matrix
A null matrix is one whose elements are all zero.
$$
N=\left[\begin{matrix}0&0&0\\0&0&0\\0&0&0\\\end{matrix}\right]
$$

#### Useful property
A useful property
$$
A.B=0
$$
we cannot say $A=0$ or $B=0$

```python
import numpy as np

A = np.array(
    [
        [3,-3,-4],
        [1, -1, -2],
        [2, -1, 4],
    ]
)

N = np.zeros(3)

print('=========================')
print('A =\n', A)
print()
print('N =\n', N)
print()
print('A * N=\n', A @ N)
print()
print('N * A=\n', N @ A)
print('=========================')

```

    =========================
    A =
     [[ 3 -3 -4]
     [ 1 -1 -2]
     [ 2 -1  4]]

    N =
     [0. 0. 0.]

    A * N=
     [0. 0. 0.]

    N * A=
     [0. 0. 0.]
    =========================

# Determinants

The arrangement of numbers in an equal rows and columns bounded by a straight bar is called determinant.

$$
\left|\begin{matrix}a_{11}&a_{12}\\a_{21}&a_{22}\\\end{matrix}\right|
$$

## Determinants of the second order
The symbol above is determinant of a $2\times 2$ (two by two matrix) with element $a_{11}$ on row 1 and column 1 and $a_{12}$  is element on row 1 and column 2.  It means each element is identified by row and column number subscript $i \textrm{and} j: a_{ij}$.

By definition,
$$
\left|\begin{matrix}a_{11}&a_{12}\\a_{21}&a_{22}\\\end{matrix}\right|=a_{11}a_{22}-a_{12}a_{21}
$$

To find determinant of
$$
\left|\begin{matrix}a_{11}&a_{12}\\a_{21}&a_{22}\\\end{matrix}\right|
$$

we must multiply the elements diagonally to form the product terms in the expansion:

$$
\textrm{we multiply }
\left|
    \begin{matrix}
        a_{11} & \\
        & a_{22}\\
    \end{matrix}
\right|

\textrm{ then subtract the product }

\left|
    \begin{matrix}
         & a_{12}\\
        a_{21} & \\
    \end{matrix}
\right|
$$

Note:
$a_{11}$ before $a_{12}$ i.e column 1 before 2.

$$
\left|
    \begin{matrix}
        1 & -3\\
        4 & -2\\
    \end{matrix}
\right|
=(1)(-2)-(-3)(4)=-2+12=10
$$

Or

$$
\left|
    \begin{matrix}
        1&\\
        &-2\\
    \end{matrix}
\right|
-
\left|
    \begin{matrix}
         & -3\\
        4 & \\
    \end{matrix}
\right|
= 1\times -2 --3\times 4 = 10
$$

More example in class

## Determinants of the third order
A determinant of the third order contains 3 rows and 3 columns.
$$
\left|\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\\\end{matrix}\right|
$$

In order to find the determinant of a higher order the matrix minor must be developed until second order is reached.
Developing each element in the first row minor:
$$
\left|\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\\\end{matrix}\right|
\left|\begin{matrix}+&-&+\\& &\\&&\\\end{matrix}\right|
$$

For $a_{11}$
$$
\left|\begin{matrix}a_{11}&&\\&a_{22}&a_{23}\\&a_{32}&a_{33}\\\end{matrix}\right|
\begin{matrix}a_{11}\\\\\end{matrix}\left|\begin{matrix}a_{22}&a_{23}\\a_{32}&a_{33}\\\end{matrix}\right|=a_{11}\left(a_{22}a_{33}-a_{23}a_{32}\right)
$$
For $a_{12}$
$$
\left|\begin{matrix}&a_{12}&\\a_{21}&&a_{23}\\a_{31}&&a_{33}\\\end{matrix}\right|
\begin{matrix}{-a}_{12}\\\\\end{matrix}\left|\begin{matrix}a_{21}&a_{23}\\a_{31}&a_{33}\\\end{matrix}\right|=-a_{12}\left(a_{21}a_{33}-a_{23}a_{31}\right)
$$
For $a_{13}$
$$
\left|\begin{matrix}&&a_{13}\\a_{21}&a_{22}&\\a_{31}&a_{32}&\\\end{matrix}\right|
\begin{matrix}a_{13}\\\\\end{matrix}\left|\begin{matrix}a_{21}&a_{22}\\a_{31}&a_{32}\\\end{matrix}\right|=a_{13}\left(a_{21}a_{32}-a_{22}a_{31}\right)
$$

Summing all together:
$$
\left|\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\\\end{matrix}\right|=\begin{matrix}a_{11}\\\\\end{matrix}\left|\begin{matrix}a_{22}&a_{23}\\a_{32}&a_{33}\\\end{matrix}\right|\begin{matrix}{-a}_{12}\\\\\end{matrix}\left|\begin{matrix}a_{21}&a_{23}\\a_{31}&a_{33}\\\end{matrix}\right|\begin{matrix}{+a}_{13}\\\\\end{matrix}\left|\begin{matrix}a_{21}&a_{22}\\a_{31}&a_{32}\\\end{matrix}\right|
$$

$$
\left|\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right|
=\begin{matrix}3\\\\\end{matrix}\left|\begin{matrix}-1&-1\\-1&4\\\end{matrix}\right|\begin{matrix}-\\\\\end{matrix}\begin{matrix}-3\\\\\end{matrix}\left|\begin{matrix}1&-1\\2&4\\\end{matrix}\right|\begin{matrix}+\\\\\end{matrix}\begin{matrix}-4\\\\\end{matrix}\left|\begin{matrix}1&-1\\2&-1\\\end{matrix}\right|
$$
$$
=3\left((-1\times4)-(-1\times-1)\right)--3((1\times4)-(-1\times2))+-4((1\times-1)-(-1\times2))
$$
$$
=3\left(-4-1\right)--3(4-(-2))+-4(-1-(-2))
$$
$$
=3\left(-5\right)+3(6)-4(1)
$$
$$
=-15+18-4=-1
$$

Try
$$
\left|\begin{matrix}2&3&4\\6&1&0\\2&3&4\\\end{matrix}\right|
$$

Answer
$$
\left|\begin{matrix}2&3&4\\6&1&0\\2&3&4\\\end{matrix}\right|=0
$$

# Solution of system of linear equations by determinants

A system of linear equations of the second degree of two unknowns can be written below:
$$
a_{11}x+a_{12}y=b_1 \\
a_{21}x+a_{22}y=b_2
$$
and can further be written in matrices form as
$$
\left[\begin{matrix}a_{11}&a_{11}\\a_{11}&a_{11}\\\end{matrix}\right]\ \ \left[\begin{matrix}x\\y\\\end{matrix}\right]=\left[\begin{matrix}b_1\\b_2\\\end{matrix}\right]\ \
$$
$$
A . x=b
$$

Solving Simultaneous Equation
$$
2x + 3y = 10\\
4x + y = 1
$$

By elimination and substitution
$$
x=-\frac{7}{10},\quad y=\frac{38}{10}
$$

## Determinant Equation Method
### Secondary Degree
$$
2x + 3y = 10\\
4x + y = 1
$$
could be written as:
$$
2x+3y-10=0 \quad \left(a\right) \\
4x+y-1=0 \quad \left(b\right)
$$
and
$$
a_{11}x+a_{12}y+b_1=0 \quad \left(a\right) \\
a_{21}x+a_{22}y+b_2=0 \quad \left(b\right)
$$

The solution
$$
\frac{x}{∆_1}=\frac{-y}{∆_2}=\frac{1}{∆_0}
$$
$$
x=\frac{∆_1}{∆_0}, \quad y=\frac{-∆_2}{∆_0}
$$
where
$$
∆_0=\left|
        \begin{matrix}
            a_{11} & a_{12} \\
            a_{21} & a_{22}
        \end{matrix}
    \right|
,\quad
∆_1=\left|
        \begin{matrix}
            a_{12} & b_{1} \\
            a_{22} & b_{2}
        \end{matrix}
    \right|
,\quad
∆_2=\left|
        \begin{matrix}
            a_{11} & b_{1} \\
            a_{21} & b_{2}
        \end{matrix}
    \right|
$$

Solving Equation
$$
2x+3y-10=0 \quad \left(a\right) \\
4x+y-1=0 \quad \left(b\right)
$$
$$
∆_0=\left|
        \begin{matrix}
            2 & 3 \\
            4 & 1
        \end{matrix}
    \right|
    =
    (2\times 1)-(3\times 4)=2-12=-10
$$
$$
∆_1=\left|
        \begin{matrix}
            3 & -10 \\
            1 & -1
        \end{matrix}
    \right|
    =
    (3\times -1)-(-10 \times 1)=-3--10=7
$$
$$
∆_2=\left|
        \begin{matrix}
            2 & -10 \\
            4 & -1
        \end{matrix}
    \right|
    =
    (2 \times -1)-(-10 \times 4)=-2--40=38
$$
$$
x=\frac{∆_1}{∆_0}=\frac{7}{-10} =\left. -\frac{7}{10} \right. \\
y=\frac{-∆_2}{∆_0}=\left.-\frac{38}{-10}\right.=\frac{38}{10}
$$

### The solution for higher degree system of linear equations are:
### For third degree:

$$
\frac {x}{∆_1}=\frac {-y}{∆_2}= \frac {z}{∆_2}=\frac {-1}{∆_0}
$$
$$
x=\frac {-∆_1}{∆_0}, \quad y=\frac {∆_2}{∆_0}, \quad z=\frac {-∆_3}{∆_0}
$$

Note: The positive and negative signs alternate has the degree increases.

### Third Degree
Solve
$$
3x -3y - 4z = 1 \\
x - y - z = 2 \\
2x-y+ 4z =3
$$

Rearrange
$$
3x -3y -4z -1= 0 \\
x - y - z -2 =0 \\
2x - y + 4z -3 = 0
$$

Extract the matrix of coefficients:

$$
\left[\begin{matrix}\begin{matrix}\begin{matrix}3\\1\\2\\\end{matrix}&\begin{matrix}-3\\-1\\-1\\\end{matrix}\\\end{matrix}&\begin{matrix}\begin{matrix}-4\\-1\\4\\\end{matrix}&\begin{matrix}-1\\-2\\-3\\\end{matrix}\\\end{matrix}\\\end{matrix}\right]
$$

```python

```

```python

```

```python

```

```python

From Example 1.2
∆0=3-3-41-1-12-14=-1

∆1=-3-4-1-1-1-2-14-3=

=\begin{matrix}-3\\\\\end{matrix}\left|\begin{matrix}-1&-2\\4&-3\\\end{matrix}\right|\begin{matrix}-\\\\\end{matrix}\begin{matrix}-4\\\\\end{matrix}\left|\begin{matrix}-1&-2\\-1&-3\\\end{matrix}\right|\begin{matrix}+\\\\\end{matrix}\begin{matrix}-1\\\\\end{matrix}\left|\begin{matrix}-1&-1\\-1&4\\\end{matrix}\right|
=-3\left(3--8\right)--4(3-2)+-1(-4-1)
=-3\left(11\right)+4(1)-(-5)=-24

∆2=3-4-11-1-224-3

=\begin{matrix}3\\\\\end{matrix}\left|\begin{matrix}-1&-2\\4&-3\\\end{matrix}\right|\begin{matrix}-\\\\\end{matrix}\begin{matrix}-4\\\\\end{matrix}\left|\begin{matrix}1&-2\\2&-3\\\end{matrix}\right|\begin{matrix}+\\\\\end{matrix}\begin{matrix}-1\\\\\end{matrix}\left|\begin{matrix}1&-1\\2&4\\\end{matrix}\right|
=3\left(3--8\right)--4(-3--4)+-1(4--2)
=3\left(11\right)+4(1)-(6)=31

∆3=3-3-11-1-22-1-3

=\begin{matrix}3\\\\\end{matrix}\left|\begin{matrix}-1&-2\\-1&-3\\\end{matrix}\right|\begin{matrix}-\\\\\end{matrix}\begin{matrix}-3\\\\\end{matrix}\left|\begin{matrix}1&-2\\2&-3\\\end{matrix}\right|\begin{matrix}+\\\\\end{matrix}\begin{matrix}-1\\\\\end{matrix}\left|\begin{matrix}1&-1\\2&-1\\\end{matrix}\right|
=3\left(3-2\right)--3(-3--4)+-1(-1--2)
=3\left(1\right)+3(1)-(1)=5

From Equation 2.13
x=-∆1∆0,    y=∆2∆0,    z=-∆3∆0

x=-\frac{-24}{-1},\ \ \ \ y=\frac{31}{-1},\ \ \ \ z=-\frac{5}{-1}
x=-24,\ \ \ \ y=-31,\ \ \ \ z=5

Cramers Rule
For second degree
For a system of two linear equations in two unknowns (x, y) given below:
a_{11}x+a_{12}y=b_1\ \ \ \ \bigma_{21}x+a_{22}y=b_2\ \ \ \
Cramer’s rule states that:
x=\frac{\left|\begin{matrix}b_1&a_{12}\\b_2&a_{22}\\\end{matrix}\right|}{\left|\begin{matrix}a_{11}&a_{12}\\a_{21}&a_{22}\\\end{matrix}\right|},\ \ \ \ y=\frac{\left|\begin{matrix}a_{11}&b_1\\a_{21}&b_2\\\end{matrix}\right|}{\left|\begin{matrix}a_{11}&a_{12}\\a_{21}&a_{22}\\\end{matrix}\right|}\
The denominator is the determinant of the coefficients of x and y. The numerator for x is the determinant of coefficient matrix when coefficient of x is replaced by b. Similarly, the numerator for y is the determinant of coefficient matrix when coefficient of y is replaced by b.

Third degree
a_{11}x+a_{12}y+a_{13}z=b_1\ \ \ \ \bigma_{21}x+a_{22}y+a_{23}z=b_2\bigma_{31}x+a_{32}y+a_{33}z=b_3
x=\frac{\left|\begin{matrix}b_1&a_{12}&a_{13}\\b_2&a_{22}&a_{23}\\b_3&a_{32}&a_{33}\\\end{matrix}\right|}{\left|\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\\\end{matrix}\right|},\ \ \ \ y=?,\ \ z=?

y=\frac{\left|\begin{matrix}a_{11}&b_1&a_{13}\\a_{21}&b_2&a_{23}\\a_{31}&b_3&a_{33}\\\end{matrix}\right|}{\left|\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\\\end{matrix}\right|},\ \ \ \
z=\frac{\left|\begin{matrix}a_{11}&a_{12}&b_1\\a_{21}&a_{22}&b_2\\a_{31}&a_{32}&b_3\\\end{matrix}\right|}{\left|\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\\\end{matrix}\right|},\ \ \ \

The Cramer’s rule can be generalized:
x=\frac{D_x}{D},\ \ \ \ \ \ \ \ \ y=\frac{D_y}{D},\ \ z=\frac{D_z}{D}\

Solve
3x\ -3y-4z=\ 1\bigmx-y-\ z\ \ \ \ \ \ \ =2\bigm2x-y+\ 4y\ \ \ =3

x=\frac{\left|\begin{matrix}1&-3&-4\\2&-1&-1\\3&-1&4\\\end{matrix}\right|}{\left|\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right|}=\frac{24}{-1}=-24
y=\frac{\left|\begin{matrix}3&1&-4\\1&2&-1\\2&3&4\\\end{matrix}\right|}{\left|\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right|}=\frac{31}{-1}=-31
z=\frac{\left|\begin{matrix}3&-3&1\\1&-1&2\\2&-1&3\\\end{matrix}\right|}{\left|\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right|}=\frac{-5}{-1}=5

Note: For both the determinant Equation Method and Cramer’s rule determinant, ∆0 or D , the coefficient of the unknown must not equal zero.
∆0≠0; D≠0

Linear dependence and independence
A matrix is said to be linearly dependent if the determinant is zero and linearly independent if the determinant is not zero.
Linear dependence
D=\left|A\right|=\ 0
And Linear independence
D=\left|A\right|\neq\ 0

Check if the matrices below are linear dependence or independence:
A=\left|\begin{matrix}2&3&4\\6&1&0\\2&3&4\\\end{matrix}\right|
B=\left|\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right|

What is your answer?
A=\left|\begin{matrix}2&3&4\\6&1&0\\2&3&4\\\end{matrix}\right|=0
B=\left|\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right|=-1

Rank of a matrix
The rank of an m x n matrix A is the order of the largest square, linear independence sub-matrix. In order to get a matrix rank you must get a non-zero determinant matrix of the original m x n matrix or a sub-matrix.

Check the rank of matrix B below:
B=\left|\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right|=-1
Since the determinant is non-zero the rank = 3.

Similarly, check for matrix A.
A=\left|\begin{matrix}2&3&4\\6&1&0\\2&3&4\\\end{matrix}\right|=0
The determinant is zero, we need to check for the determinant of its sub-matrix:
A_1=\left|\begin{matrix}2&3\\6&1\\\end{matrix}\right|=-16
Since the determinant of the sub-matrix A1 is non-zero, we can conclude that matrix A is of rank =2.

General system of linear equations,
A system linear of equations below with three unknown x, y, z:
a_{11}x_1+a_{12}x_2+a_{13}x_3=b_1\ \ \ \ \bigma_{21}x_1+a_{22}x_2+a_{23}x_3=b_2\bigma_{31}x_1+a_{32}x_2+a_{33}x_3=b_3
can be represented using matrix notation:
\left[\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\\\end{matrix}\right]\left[\begin{matrix}x_1\\x_2\\x_3\\\end{matrix}\right]=\left[\begin{matrix}b_1\\b_2\\b_3\\\end{matrix}\right]

Similarly,
a_{11}x_1+a_{12}x_2+a_{13}x_3=b_1\ \ \ \ \bigma_{21}x_1+a_{22}x_2+a_{23}x_3=b_2
\left[\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\\end{matrix}\right]\left[\begin{matrix}x_1\\x_2\\x_3\\\end{matrix}\right]=\left[\begin{matrix}b_1\\b_2\\\end{matrix}\right]

Generally,
\begin{matrix}\begin{matrix}\begin{matrix}a_{11}x_1\\a_{21}x_1\\a_{31}x_1\\\end{matrix}&\begin{matrix}a_{12}x_2\\a_{22}x_2\\a_{32}x_2\\\end{matrix}&\begin{matrix}a_{13}x_3\\a_{23}x_3\\a_{33}x_3\\\end{matrix}\\\end{matrix}&\begin{matrix}\ldots\\\ldots\\\ldots\\\end{matrix}&\begin{matrix}\begin{matrix}a_{1n}x_n&=&b_1\\\end{matrix}\\\begin{matrix}a_{2n}x_n&=&b_1\\\end{matrix}\\\begin{matrix}a_{3n}x_n&=&b_1\\\end{matrix}\\\end{matrix}\\\begin{matrix}\vdots\ \ \ \ \ &\ \ \ \vdots\ \ &\ \ \ \ \ \ \ \ \vdots\\\end{matrix}&&\ \ \ \begin{matrix}\vdots&&\ \ \ \ \ \ \vdots\\\end{matrix}\\\begin{matrix}a_{m1}x_1&a_{m2}x_2&a_{m3}x_3\\\end{matrix}&\ldots&\begin{matrix}a_{mn}x_n&=&b_m\\\end{matrix}\\\end{matrix}
\left[\begin{matrix}\begin{matrix}\begin{matrix}a_{11}\\a_{21}\\a_{31}\\\end{matrix}&\begin{matrix}a_{12}\\a_{22}\\a_{32}\\\end{matrix}&\begin{matrix}a_{13}\\a_{23}\\a_{33}\\\end{matrix}\\\end{matrix}&\begin{matrix}\ldots\\\ldots\\\ldots\\\end{matrix}&\begin{matrix}a_{1n}\\a_{2n}\\a_{3n}\\\end{matrix}\\\begin{matrix}\vdots\ \ &\ \ \ \vdots\ &\ \ \ \ \vdots\\\end{matrix}&&\vdots\\\begin{matrix}a_{m1}&a_{m2}&a_{m3}\\\end{matrix}&\ldots&a_{mn}\\\end{matrix}\right]\left[\begin{matrix}x_1\\\begin{matrix}x_2\\x_3\\\vdots\\\end{matrix}\\x_n\\\end{matrix}\right]=\left[\begin{matrix}b_1\\\begin{matrix}b_2\\b_3\\\vdots\\\end{matrix}\\b_m\\\end{matrix}\right]
A\bullet x=b
where
A=\left[\begin{matrix}\begin{matrix}\begin{matrix}a_{11}\\a_{21}\\a_{31}\\\end{matrix}&\begin{matrix}a_{12}\\a_{22}\\a_{32}\\\end{matrix}&\begin{matrix}a_{13}\\a_{23}\\a_{33}\\\end{matrix}\\\end{matrix}&\begin{matrix}\ldots\\\ldots\\\ldots\\\end{matrix}&\begin{matrix}a_{1n}\\a_{2n}\\a_{3n}\\\end{matrix}\\\begin{matrix}\vdots\ \ &\ \ \ \vdots\ &\ \ \ \ \vdots\\\end{matrix}&&\vdots\\\begin{matrix}a_{m1}&a_{m2}&a_{m3}\\\end{matrix}&\ldots&a_{mn}\\\end{matrix}\right];\ \ x=\left[\begin{matrix}x_1\\\begin{matrix}x_2\\x_3\\\vdots\\\end{matrix}\\x_n\\\end{matrix}\right];\ \ b=\left[\begin{matrix}b_1\\\begin{matrix}b_2\\b_3\\\vdots\\\end{matrix}\\b_m\\\end{matrix}\right]
If we multiply Eq (6.7) by A-1 the inverse of A.
A^{-1}.A.x=A^{-1}.b
A^{-1}.A=\mathbit{I}
\mathbit{I}.x=A^{-1}.b
x=A^{-1}.b
and
A^{-1}=\frac{C^T}{\left|A\right|}

Existence and properties of solution
Existence of solution
Looking at the equations below:
A.x=b
x=A^{-1}.b
A^{-1}=\frac{C^T}{\left|A\right|}
Forming an augmented matrix A_b.
A_b.=[A|b]

For a solution to exist for a set of linear equations the following must be true:
	Determinant of A must not be equal to zero, i.e \left|A\right|\neq0.
	A unique solution exists:
rank\ of\ A=rank\ of\ A_b=n
	An infinite number of solutions exist:
rank\ of\ A=rank\ of\ A_b=m<n
	No solution exists:
rank\ A<rank\ A_b
Other properties of the solution
x=0,\ \ A^{-1}=0\ or\ b=0

Gaussian elimination.
Algorithm of Gaussian Elimination
Gaussian elimination method converts the combine matrix of A and b, [A|b] to triangular matrix. Then proceed with backward substitution to obtain x.
For a system of linear equations
a_{11}x_1+a_{12}x_2+a_{13}x_3=b_1\ \ \ \ \bigma_{21}x_1+a_{22}x_2+a_{23}x_3=b_2\bigma_{31}x_1+a_{32}x_2+a_{33}x_3=b_3

Step 1: Write in matrix form.
\left[\begin{matrix}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\\\end{matrix}\right]\left[\begin{matrix}x_1\\x_2\\x_3\\\end{matrix}\right]=\left[\begin{matrix}b_1\\b_2\\b_3\\\end{matrix}\right]
A\bullet x=b

Step 2: Arrange matrix A and b together to form augmented matrix A_b.
A_b=\left[A|b\right]
A_b=\left[\begin{matrix}a_{11}&a_{12}&\begin{matrix}a_{13}&|&b_1\\\end{matrix}\\a_{21}&a_{22}&\begin{matrix}a_{23}&|&b_2\\\end{matrix}\\a_{31}&a_{32}&\begin{matrix}a_{33}&|&b_3\\\end{matrix}\\\end{matrix}\right]
Step 3: Using matrix row equivalent operation reduce A_b to triangular matrix \widetilde{A_b}.
A_b\rightarrow\widetilde{A_b}
\left[\begin{matrix}a_{11}&a_{12}&\begin{matrix}a_{13}&|&b_1\\\end{matrix}\\a_{21}&a_{22}&\begin{matrix}a_{23}&|&b_2\\\end{matrix}\\a_{31}&a_{32}&\begin{matrix}a_{33}&|&b_3\\\end{matrix}\\\end{matrix}\right]\rightarrow\left[\begin{matrix}{\widetilde{a}}_{11}&{\widetilde{a}}_{12}&\begin{matrix}{\widetilde{a}}_{13}&|&{\widetilde{b}}_1\\\end{matrix}\\0&{\widetilde{a}}_{22}&\begin{matrix}{\widetilde{a}}_{23}&|&{\widetilde{b}}_2\\\end{matrix}\\0&0&\begin{matrix}{\widetilde{a}}_{33}&|&{\widetilde{b}}_3\\\end{matrix}\\\end{matrix}\right]
\widetilde{M}=\left[\begin{matrix}{\widetilde{a}}_{11}&{\widetilde{a}}_{12}&\begin{matrix}{\widetilde{a}}_{13}&|&{\widetilde{b}}_1\\\end{matrix}\\0&{\widetilde{a}}_{22}&\begin{matrix}{\widetilde{a}}_{23}&|&{\widetilde{b}}_2\\\end{matrix}\\0&0&\begin{matrix}{\widetilde{a}}_{33}&|&{\widetilde{b}}_3\\\end{matrix}\\\end{matrix}\right]
Step 4: Back substitution
\begin{matrix}\begin{matrix}{\widetilde{a}}_{11}x_1&+\\\end{matrix}&\begin{matrix}{\widetilde{a}}_{12}x_2&+\\\end{matrix}&\begin{matrix}{\widetilde{a}}_{13}x_3&=&{\widetilde{b}}_1\\\end{matrix}\\&\begin{matrix}{\widetilde{a}}_{22}x_2&+\\\end{matrix}&\begin{matrix}{\widetilde{a}}_{23}x_3&=&{\widetilde{b}}_2\\\end{matrix}\\&&\begin{matrix}{\widetilde{a}}_{33}x_3&=&{\widetilde{b}}_3\\\end{matrix}\\\end{matrix}
\begin{matrix}x_3&=&\sfrac{{\widetilde{b}}_3}{{\widetilde{a}}_{33}}\\\end{matrix}
\begin{matrix}x_2&=&\frac{{\widetilde{b}}_2-{\widetilde{a}}_{23}x_3}{{\widetilde{a}}_{22}}\\\end{matrix}
\begin{matrix}x_1&=&\frac{{\widetilde{b}}_1-{\widetilde{a}}_{12}x_2-{\widetilde{a}}_{13}x_3}{{\widetilde{a}}_{11}}\\\end{matrix}

Solve the set of equations.
3x\ -3y-4z=\ 1\bigmx-y-\ z\ \ \ \ \ \ \ =2\bigm2x-y+\ 4y\ \ \ =3
Step 1: Write in matrix form.
A\bullet x=b
\left[\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right]\left[\begin{matrix}x\\y\\z\\\end{matrix}\right]=\left[\begin{matrix}1\\2\\3\\\end{matrix}\right]

Step 2: Arrange matrix A and b together augmented matrix A_b.
A_b=\left[A|b\right]
A_b=\left[\begin{matrix}3&-3&\begin{matrix}-4&|&1\\\end{matrix}\\1&-1&\begin{matrix}-1&|&2\\\end{matrix}\\2&-1&\begin{matrix}\ \ \ 4&|&3\\\end{matrix}\\\end{matrix}\right]
Step 3: Using matrix row equivalent operation reduce M to triangular matrix \widetilde{M}.
A_b\rightarrow\widetilde{A_b}
pivot,\ p=1,\ldots,\ m-1\
i=p+1,\ ...,\ m\ \ \ \
Note:
a_{pp}\neq0\ \ \ \
If a_{pp}\neq0\ \ \ \ \ interchange\ row\ with\ a\ none-zero\ pivot\ column\ element.
R_i\ =R_i-\left(R_p\times\ \frac{a_{ip}}{a_{pp}}\right)\ \ \

p=1,...,2;\ i=2,...,3
Reduce {\widetilde{A_b}}_{p=1}
\ p=1;\ i=2
a_{pp}=a_{11}=3\ \ \ \
R_2=\left[\begin{matrix}1&-1&\begin{matrix}-1&|&2\\\end{matrix}\\\end{matrix}\right]-\left(\left[\begin{matrix}3&-3&\begin{matrix}-4&|&1\\\end{matrix}\\\end{matrix}\right]\times\frac{1}{3}\right)=\left[\begin{matrix}0&0&\begin{matrix}0.33&|&1.67\\\end{matrix}\\\end{matrix}\right]
p=1;\ i=3\ \ \ \
R_3=\left[\begin{matrix}2&-1&\begin{matrix}4&|&3\\\end{matrix}\\\end{matrix}\right]-\left(\left[\begin{matrix}3&-3&\begin{matrix}-4&|&1\\\end{matrix}\\\end{matrix}\right]\times\frac{2}{3}\right)=\left[\begin{matrix}0&1&\begin{matrix}6.67&|&2.33\\\end{matrix}\\\end{matrix}\right]
{\widetilde{A_b}}_{p=1}=\left[\begin{matrix}3&-3&\begin{matrix}-4&|&1\\\end{matrix}\\0&0&\begin{matrix}0.33&|&1.67\\\end{matrix}\\0&1&\begin{matrix}6.67&|&2.33\\\end{matrix}\\\end{matrix}\right]
Reduce {\widetilde{A_b}}_{p=2}
p=2;\ i=3\ \ \ \
a_{pp}=a_{22}=0\ \ \ \
Interchange row 2 and 3
{\widetilde{A_b}}_{p=1}=\left[\begin{matrix}3&-3&\begin{matrix}-4&|&1\\\end{matrix}\\0&1&\begin{matrix}6.67&|&2.33\\\end{matrix}\\0&0&\begin{matrix}0.33&|&1.67\\\end{matrix}\\\end{matrix}\right]
\ p=2;\ i=3\ \ \ \
a_{pp}=a_{22}=1\ \ \ \
R_3=\left[\begin{matrix}0&0&\begin{matrix}0.33&|&1.67\\\end{matrix}\\\end{matrix}\right]-\left(\left[\begin{matrix}0&1&\begin{matrix}6.67&|&2.33\\\end{matrix}\\\end{matrix}\right]\times\frac{0}{1}\right)=\left[\begin{matrix}0&0&\begin{matrix}0.33&|&1.67\\\end{matrix}\\\end{matrix}\right]
{\widetilde{A_b}}_{p=2}=\left[\begin{matrix}3&-3&\begin{matrix}-4&|&1\\\end{matrix}\\0&1&\begin{matrix}6.67&|&2.33\\\end{matrix}\\0&0&\begin{matrix}0.33&|&1.67\\\end{matrix}\\\end{matrix}\right]

Step 4: Back substitution
\begin{matrix}\begin{matrix}3x_1&-\\\end{matrix}&\begin{matrix}3x_2&-\\\end{matrix}&\begin{matrix}4x_3&=&1\\\end{matrix}\\&\begin{matrix}x_2&+\\\end{matrix}&\begin{matrix}6.67x_3&=&2.33\\\end{matrix}\\&&\begin{matrix}0.33x_3&=&1.67\\\end{matrix}\\\end{matrix}
\begin{matrix}x_3&=&\sfrac{1.67}{0.33}\\\end{matrix}=5.06
\begin{matrix}x_2&=&\frac{2.33-6.67(5.06)}{1}\\\end{matrix}=-31.42
\begin{matrix}x_1&=&\frac{1--3(-31.42)--4(5.06)}{3}\\\end{matrix}=-24.34

Determinant from Gaussian Elimination
\widetilde{A_b}=\left[\begin{matrix}{\widetilde{a}}_{11}&{\widetilde{a}}_{12}&\begin{matrix}{\widetilde{a}}_{13}&|&{\widetilde{b}}_1\\\end{matrix}\\0&{\widetilde{a}}_{22}&\begin{matrix}{\widetilde{a}}_{23}&|&{\widetilde{b}}_2\\\end{matrix}\\0&0&\begin{matrix}{\widetilde{a}}_{33}&|&{\widetilde{b}}_3\\\end{matrix}\\\end{matrix}\right]
\left|A\right|={(-1)}^k\prod_{i}^{m}{\widetilde{a}}_{ii}={\widetilde{a}}_{11}\times{\widetilde{a}}_{22}\times{\widetilde{a}}_{33}
where k is the number of times rows were interchanged.
From the example above and Eq (8.34)
\left|A\right|={(-1)}^1\prod_{i=1}^{m=3}{\widetilde{a}}_{ii}={(-1)}^1(3\times1\times0.33)=-0.99

Matrix inverse by elementary matrices, adjoint, and partitioning methods.
Inverse of a square matrix
Recall the solution to system of linear equation:
A.x=b
x=A^{-1}.b
A^{-1}=\frac{C^T}{\left|A\right|}
To solve Eq (9.3) the inverse of matrix A, {(A}^{-1}) must be evaluated.
where C^Tis the adjoint of matrix A, and Adjoint is the transpose of the co-factor of matrix A.

Cofactors
Cofactor of a square matrix is obtained from the determinant of its elements.
\left|A\right|=\left|\begin{matrix}\begin{matrix}\begin{matrix}a_{11}\\a_{21}\\a_{31}\\\end{matrix}&\begin{matrix}a_{12}\\a_{22}\\a_{32}\\\end{matrix}&\begin{matrix}a_{13}\\a_{23}\\a_{33}\\\end{matrix}\\\end{matrix}&\begin{matrix}\ldots\\\ldots\\\ldots\\\end{matrix}&\begin{matrix}a_{1n}\\a_{2n}\\a_{3n}\\\end{matrix}\\\begin{matrix}\vdots\ \ &\ \ \ \vdots\ &\ \ \ \ \vdots\\\end{matrix}&&\vdots\\\begin{matrix}a_{n1}&a_{n2}&a_{n3}\\\end{matrix}&\ldots&a_{nn}\\\end{matrix}\right|
Cofactor=\left[\begin{matrix}\begin{matrix}\begin{matrix}c_{11}\\c_{21}\\c_{31}\\\end{matrix}&\begin{matrix}c_{12}\\c_{22}\\c_{32}\\\end{matrix}&\begin{matrix}c_{13}\\c_{23}\\c_{33}\\\end{matrix}\\\end{matrix}&\begin{matrix}\ldots\\\ldots\\\ldots\\\end{matrix}&\begin{matrix}c_{1n}\\c_{2n}\\c_{3n}\\\end{matrix}\\\begin{matrix}\vdots\ \ &\ \ \ \vdots\ &\ \ \ \ \vdots\\\end{matrix}&&\vdots\\\begin{matrix}c_{n1}&c_{n2}&c_{n3}\\\end{matrix}&\ldots&c_{nn}\\\end{matrix}\right]
where each element of the cofactor is formed from the determinant of its sub-matrix multiplied by element position sign. The element positional sign alternate between plus + and minus -.
Evaluate the cofactor of the matrix A below:
A=\left[\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right]
For element a_{11}=3;
c_{11}=+\left|\begin{matrix}-1&-1\\-1&4\\\end{matrix}\right|=-5
For element a_{12}=-3;
c_{12}=-\left|\begin{matrix}1&-1\\2&4\\\end{matrix}\right|=-6
For element a_{13}=-4;
c_{13}=+\left|\begin{matrix}1&-1\\2&-1\\\end{matrix}\right|=1
For element a_{21}=1;
c_{21}=-\left|\begin{matrix}-3&-4\\-1&4\\\end{matrix}\right|=16
For element a_{22}=-1;
c_{22}=+\left|\begin{matrix}3&-4\\2&4\\\end{matrix}\right|=20
For element a_{23}=-1;
c_{23}=-\left|\begin{matrix}3&-3\\2&-1\\\end{matrix}\right|=-3
For element a_{31}=2;
c_{31}=+\left|\begin{matrix}-3&-4\\-1&-1\\\end{matrix}\right|=-1
For element a_{32}=-1;
c_{32}=-\left|\begin{matrix}3&-4\\1&-1\\\end{matrix}\right|=-1
For element a_{33}=4;
c_{13}=+\left|\begin{matrix}3&-3\\1&-1\\\end{matrix}\right|=0
The cofactor form is:
Cofactor\ A=\left[\begin{matrix}-5&-6&1\\16&20&-3\\-1&-1&0\\\end{matrix}\right]

Adjoint
Adjoint of matrix A is the transpose of the cofactor of A.
From Eq (9.16)
Adj\ A=C^T\ =\left[\begin{matrix}-5&16&-1\\-6&20&-1\\1&-3&0\\\end{matrix}\right]

A^{-1}=\frac{C^T}{\left|A\right|}
\left|A\right|=\left|\begin{matrix}3&-3&-4\\1&-1&-1\\2&-1&4\\\end{matrix}\right|=-1
Therefore
A^{-1}=\frac{1}{-1}\left[\begin{matrix}-5&16&-1\\-6&20&-1\\1&-3&0\\\end{matrix}\right]=\left[\begin{matrix}5&-16&1\\6&-20&1\\-1&3&0\\\end{matrix}\right]
To solve the system linear of equation
x=A^{-1}.b
\left[\begin{matrix}x\\y\\z\\\end{matrix}\right]=\left[\begin{matrix}5&-16&1\\6&-20&1\\-1&3&0\\\end{matrix}\right]\left[\begin{matrix}1\\2\\3\\\end{matrix}\right]
\left[\begin{matrix}x\\y\\z\\\end{matrix}\right]=\left[\begin{matrix}(5\times1)+(-16\times2)+(1\times3)\\(6\times1)+(-20\times2)+(1\times3)\\(-1\times1)+(3\times2)+(0\times3)\\\end{matrix}\right]=\left[\begin{matrix}-24\\-31\\5\\\end{matrix}\right]

Eigenvalues and Eigenvectors.
Eigenvalues
Eigenvalues are characteristic scalar value (\lambda) of a square matrix (A) that satisfy the equation 10.1.
Ax=\lambda x
In other to find the eigenvalues, we develop the characteristic equation and solve the characteristic polynomial of matrix A.
(A-\lambda\mathbit{I})x=0
To prevent the trivial solution x\neq0. Therefore we have the characteristic equation Eq (10.3).
\left|A-\lambda\mathbit{I}\right|=0
The solution of Eq (10.3) gives the eigenvalues \lambda.

Example
Find the eigenvalues of the matrix.
A=\left[\begin{matrix}2&3\\4&1\\\end{matrix}\right]
A-\lambda\mathbit{I}=\left[\begin{matrix}2&3\\4&1\\\end{matrix}\right]-\lambda\left[\begin{matrix}1&0\\0&1\\\end{matrix}\right]=\left[\begin{matrix}(2-\lambda)&3\\4&(1-\lambda)\\\end{matrix}\right]
\left|A-\lambda\mathbit{I}\right|=\left|\begin{matrix}\left(2-\lambda\right)&3\\4&\left(1-\lambda\right)\\\end{matrix}\right|=\left(2-\lambda\right)\left(1-\lambda\right)-12=2-2\lambda-\lambda+\lambda^2-12
\left|A-\lambda\mathbit{I}\right|=\lambda^2-3\lambda-10=0
The solutions to the characteristic polynomial in Eq 10.6
\lambda=-2\ or\ 5
\lambda_1=-2,\ \ \lambda_2=5

Eigenvectors
Each eigenvalues obtained has a corresponding eigenvector or eigenline associated with it.
Ax=\lambda x
Example
When matrix A is:
A=\left[\begin{matrix}2&3\\4&1\\\end{matrix}\right]
\lambda_1=-2,\ \ \lambda_2=5
For \lambda_1=-2
\left[\begin{matrix}2&3\\4&1\\\end{matrix}\right]\left[\begin{matrix}x_1\\x_2\\\end{matrix}\right]=-2\left[\begin{matrix}x_1\\x_2\\\end{matrix}\right]
\begin{matrix}\begin{matrix}2x_1&+\\\end{matrix}&\begin{matrix}3x_2&=\\\end{matrix}\ \ \\\begin{matrix}4x_1&+\\\end{matrix}&\begin{matrix}x_2&=\\\end{matrix}\\\end{matrix}\begin{matrix}{-2x}_1\\{-2x}_2\\\end{matrix}
x_2=-\frac{4}{3}x_1
The eigenvector for \lambda_1=-2
x_1=\left[\begin{matrix}k\\-{\frac{4}{3}}k\\\end{matrix}\right]\ and\ the\ smallest:\ x_1=\left[\begin{matrix}1\\-{\frac{4}{3}}\\\end{matrix}\right]
For \lambda_2=5
\left[\begin{matrix}2&3\\4&1\\\end{matrix}\right]\left[\begin{matrix}x_1\\x_2\\\end{matrix}\right]=5\left[\begin{matrix}x_1\\x_2\\\end{matrix}\right]
\begin{matrix}\begin{matrix}2x_1&+\\\end{matrix}&\begin{matrix}3x_2&=\\\end{matrix}\ \ \\\begin{matrix}4x_1&+\\\end{matrix}&\begin{matrix}x_2&=\\\end{matrix}\\\end{matrix}\begin{matrix}{5x}_1\\{5x}_2\\\end{matrix}
x_2=x_1
The eigenvector for \lambda_2=5
x_2=\left[\begin{matrix}k\\k\\\end{matrix}\right]\ and\ the\ smallest:\ x_2=\left[\begin{matrix}1\\1\\\end{matrix}\right]

```

```python

```


# 5. Curl-Div operator

We consider the following differential operator, in its strong form, on the unit square or cube $\Omega$:

$$
\mathcal{L}^{\alpha, \beta} \mathbf{u} := \alpha \nabla \times \nabla \mathbf{u} - \beta \nabla (\nabla \cdot \mathbf{u})
$$

where $\mathbf{u}$ is supposed to be in $H^1(\Omega)$ (or $H_0^1(\Omega)$).

For our experiments, we will set $\alpha = 1$.

```python
# needed imports
from numpy import linspace, meshgrid, pi, zeros, asarray

import matplotlib.pyplot as plt
%matplotlib inline

from IPython.display import Math

from sympy.core.containers import Tuple
from sympy import symbols
from sympy import Symbol
from sympy import Lambda
from sympy import IndexedBase

from gelato.glt import glt_symbol
from gelato.glt import glt_lambdify
from gelato.glt import glt_approximate_eigenvalues
from gelato.printing.latex import glt_latex
from gelato.calculus   import (Dot, Cross, Grad, Curl, Rot, Div)
from gelato.calculus   import Constant
from gelato.fem.assembly import assemble_matrix
from gelato.fem.utils    import compile_kernel
from gelato.fem.utils    import compile_symbol

from spl.fem.splines import SplineSpace
from spl.fem.tensor  import TensorFemSpace
from spl.fem.vector  import VectorFemSpace
```

## 2-D case

The weak formulation writes:

$$
a(\mathbf{v},\mathbf{u}) = \int_{\Omega} \alpha \left( \mathrm{rot}~ \mathbf{v} \right)  \left( \mathrm{rot}~ \mathbf{u} \right) + \beta \left( \nabla \cdot \mathbf{v} \right) \left( \nabla \cdot \mathbf{u} \right) ~d\Omega
$$

```python
x,y = symbols('x y')

u = IndexedBase('u')
v = IndexedBase('v')

alpha = Constant('alpha')
beta = Constant('beta')

a = Lambda((x,y,v,u), alpha * Rot(v) * Rot(u) + beta * Div(v) * Div(u))
```

```python
# ...  create a finite element space
p1  = 3 ; p2  = 3
ne1 = 32 ; ne2 = 32

grid_1 = linspace(0., 1., ne1+1)
grid_2 = linspace(0., 1., ne2+1)

V1 = SplineSpace(p1, grid=grid_1)
V2 = SplineSpace(p2, grid=grid_2)

V = TensorFemSpace(V1, V2)
# ...

# ... create a vector fem space
V = VectorFemSpace(V, V)
# ...
```

Let's first compute the GLT-symbol, without evaluating it *symbolicaly*:

```python
from IPython.display import Math
from gelato.printing.latex import glt_latex
from sympy import latex

symbol = glt_symbol(a, space=V, evaluate=False)

Math(latex(symbol))
#Math(glt_latex(symbol))
```

$$\left[\begin{matrix}\alpha m_{1} s_{2} + \beta m_{2} s_{1} & \alpha a_{1} a_{2} - \beta a_{1} a_{2}\\\alpha a_{1} a_{2} - \beta a_{1} a_{2} & \alpha m_{2} s_{1} + \beta m_{1} s_{2}\end{matrix}\right]$$

```python
d_constants = {'alpha': 1.}
d_types = {'beta': 'double'}
```

We define the FEM kernel, needed to assemble the associated matrix

```python
kernel_f90 = compile_kernel('kernel_2d', a, V,
                            d_constants=d_constants,
                            d_args=d_types,
                            backend='fortran')
```

For our experiment, we will use the following value for $\beta$

```python
d_args = {'beta': 1.e-3}
```

We assemble the FEM matrix

```python
M = assemble_matrix(V, kernel_f90, args=d_args)
```

```python
from utils import coo_from_blocks

n_block_rows = 2 ; n_block_cols = 2
matrices = []
for ir in range(0, n_block_rows):
    line = []
    for ic in range(0, n_block_cols):
        coo = M[ir][ic].tocoo()
        coo.eliminate_zeros()
        line.append(coo)
    matrices.append(line)
_M = M
M = coo_from_blocks(matrices,
                    n_block_rows=n_block_rows,
                    n_block_cols=n_block_cols)
```

We compute the eigenvalues, using *Lapack*

```python
# import eigenvalue solver
from scipy.linalg import eig

M = M.todense()
w, v = eig(M)
wr = w.real
wr.sort()
```

We now evaluate oyr symbol, to prepare for the classical comparison with the FEM matrix

```python
symbol_f90 = compile_symbol('symbol_2d', a, V,
                            d_constants=d_constants,
                            d_args=d_types,
                            backend='fortran')
```

Sampling the GLT symbol in the Fourier space, is done as usual by calling:

```python
# ... example of symbol evaluation
n1 = V1.nbasis
n2 = V2.nbasis
t1 = linspace(-pi,pi, n1)
t2 = linspace(-pi,pi, n2)
x1 = linspace(0.,1., n1)
x2 = linspace(0.,1., n2)
e = zeros((2, 2, n1, n2), order='F')
symbol_f90(x1,x2,t1,t2, e, d_args['beta'])
# ...
```

```python
W = []
for i1 in range(0, n1):
    for i2 in range(0, n2):
        mat = e[:,:,i1,i2]
        w,v = eig(mat)
        W += list(w.real)
t = asarray(W)
t.sort()
```

```python
plt.plot(t, "+b", label="glt symbol")
plt.plot(wr, "xr", label="eigenvalues")
plt.legend(loc=2);
```

## 3-D case

The weak formulation writes:

$$
a(\mathbf{v},\mathbf{u}) = \int_{\Omega} \alpha \left( \nabla \times \mathbf{v} \right)  \left( \nabla \times \mathbf{u} \right) + \beta \left( \nabla \cdot \mathbf{v} \right) \left( \nabla \cdot \mathbf{u} \right) ~d\Omega
$$

```python
x,y,z = symbols('x y z')

u = IndexedBase('u')
v = IndexedBase('v')

alpha = Constant('alpha')
beta = Constant('beta')

a = Lambda((x,y,z,v,u), alpha * Dot(Curl(u), Curl(v)) + beta * Div(v) * Div(u))
```

```python
# ...  create a finite element space
p1  = 2 ; p2  = 2 ; p3  = 2
ne1 = 8 ; ne2 = 8 ; ne3 = 8

grid_1 = linspace(0., 1., ne1+1)
grid_2 = linspace(0., 1., ne2+1)
grid_3 = linspace(0., 1., ne3+1)

V1 = SplineSpace(p1, grid=grid_1)
V2 = SplineSpace(p2, grid=grid_2)
V3 = SplineSpace(p3, grid=grid_3)

W = TensorFemSpace(V1, V2, V3)
# ...

V = VectorFemSpace(W, W, W)
```

Let's first compute the GLT symbol and print it

```python
from IPython.display import Math
from gelato.printing.latex import glt_latex
from sympy import latex

symbol = glt_symbol(a, space=V, evaluate=False)

Math(latex(symbol))
#Math(glt_latex(symbol))
```

$$\left[\begin{matrix}\alpha m_{1} m_{2} s_{3} + \alpha m_{1} m_{3} s_{2} + \beta m_{2} m_{3} s_{1} & \alpha a_{1} a_{2} m_{3} - \beta a_{1} a_{2} m_{3} & \alpha a_{1} a_{3} m_{2} - \beta a_{1} a_{3} m_{2}\\\alpha a_{1} a_{2} m_{3} - \beta a_{1} a_{2} m_{3} & \alpha m_{1} m_{2} s_{3} + \alpha m_{2} m_{3} s_{1} + \beta m_{1} m_{3} s_{2} & \alpha a_{2} a_{3} m_{1} - \beta a_{2} a_{3} m_{1}\\\alpha a_{1} a_{3} m_{2} - \beta a_{1} a_{3} m_{2} & \alpha a_{2} a_{3} m_{1} - \beta a_{2} a_{3} m_{1} & \alpha m_{1} m_{3} s_{2} + \alpha m_{2} m_{3} s_{1} + \beta m_{1} m_{2} s_{3}\end{matrix}\right]$$

```python
d_constants = {'alpha': 1.}
d_types = {'beta': 'double'}
```

```python
kernel_f90 = compile_kernel('kernel_3d', a, V,
                            d_constants=d_constants,
                            d_args=d_types,
                            backend='fortran')
```

```python
symbol_f90 = compile_symbol('symbol_3d', a, V,
                            d_constants=d_constants,
                            d_args=d_types,
                            backend='fortran')
```

```python
d_args = {'beta': 1.e-3}
```

```python
M = assemble_matrix(V, kernel_f90, args=d_args)
```

```python
from utils import coo_from_blocks

n_block_rows = 3 ; n_block_cols = 3
matrices = []
for ir in range(0, n_block_rows):
    line = []
    for ic in range(0, n_block_cols):
        coo = M[ir][ic].tocoo()
        coo.eliminate_zeros()
        line.append(coo)
    matrices.append(line)
_M = M
M = coo_from_blocks(matrices,
                    n_block_rows=n_block_rows,
                    n_block_cols=n_block_cols)
```

```python
# import eigenvalue solver
from scipy.linalg import eig

M = M.todense()
w, v = eig(M)
wr = w.real
wr.sort()
```

```python
# ... example of symbol evaluation
n1 = V1.nbasis
n2 = V2.nbasis
n3 = V3.nbasis
t1 = linspace(-pi,pi, n1)
t2 = linspace(-pi,pi, n2)
t3 = linspace(-pi,pi, n3)
x1 = linspace(0.,1., n1)
x2 = linspace(0.,1., n2)
x3 = linspace(0.,1., n3)
e = zeros((3, 3, n1, n2, n3), order='F')
symbol_f90(x1,x2,x3,t1,t2,t3, e, d_args['beta'])
# ...
```

```python
W = []
for i1 in range(0, n1):
    for i2 in range(0, n2):
        for i3 in range(0, n3):
            mat = e[:,:,i1,i2,i3]
            w,v = eig(mat)
            W += list(w.real)
t = asarray(W)
t.sort()
```

```python
plt.plot(t, "+b", label="glt symbol")
plt.plot(wr, "xr", label="eigenvalues")
plt.legend(loc=2);
```

```python

```

```python
from IPython.core.display import HTML
def css_styling():
    styles = open("../../styles/custom.css", "r").read()
    return HTML(styles)
css_styling()
```

<link href='http://fonts.googleapis.com/css?family=Fenix' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Alegreya+Sans:100,300,400,500,700,800,900,100italic,300italic,400italic,500italic,700italic,800italic,900italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:300,400' rel='stylesheet' type='text/css'>
<style>
    @font-face {
        font-family: "Computer Modern";
        src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');
    }
    div.cell{
        width:600px;
        margin-left:16% !important;
        margin-right:auto;
    }
    h1 {
        font-family: 'Alegreya Sans', sans-serif;
    }
    h2 {
        font-family: 'Fenix', serif;
    }
    h3{
		font-family: 'Fenix', serif;
        margin-top:12px;
        margin-bottom: 3px;
       }
	h4{
		font-family: 'Fenix', serif;
       }
    h5 {
        font-family: 'Alegreya Sans', sans-serif;
    }
    div.text_cell_render{
        font-family: 'Alegreya Sans',Computer Modern, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
        line-height: 135%;
        font-size: 120%;
        width:600px;
        margin-left:auto;
        margin-right:auto;
    }
    .CodeMirror{
            font-family: "Source Code Pro";
			font-size: 90%;
    }
/*    .prompt{
        display: None;
    }*/
    .text_cell_render h1 {
        font-weight: 200;
        font-size: 50pt;
		line-height: 100%;
        color:#054BCD;
        margin-bottom: 0.5em;
        margin-top: 0.5em;
        display: block;
    }
    .text_cell_render h5 {
        font-weight: 300;
        font-size: 16pt;
        color: #054BCD;
        font-style: italic;
        margin-bottom: .5em;
        margin-top: 0.5em;
        display: block;
    }

    .warning{
        color: rgb( 240, 20, 20 )
        }
</style>

```python

```