Linear Algebra
Overview
This repository contains a package that provides a collection of functions and documentation for learning and applying linear algebra concepts. The code and documentation were created as I worked through various linear algebra textbooks, discussed below.
The code in this package is not meant to be a complete implementation of the materials in the books, but rather code that I created in order to gain practice and a deeper understanding of the concepts in the books. The code is not necessarily divided in the same way as the documentation. I actually worked through the basics book after I was well into the linear algebra books, so the Julia code files are mostly related to the Geometry books. By contrast, in the Github repository, you will find notebooks related to each of the documentation sections.
Besides the package documentation, you will also find in the left navigation tab links to documents explaining topics from the source books. These often provide more details than covered in the books. Currently, there are two sections: Basics and Geometry.
The Basics documentation section covers the algebraic foundations of linear algebra, including vectors, matrices, systems of equations and analytic geometry. The source of the documents in this section is the book Foundations of Mathematics: A Preparatory Course. by Guido Walz, Frank Zeilfelder and Thomas Rießinger. The earlier contents of this book cover basic mathematical concepts, and documentation and code for those topics can be found in the Math_Foundations package.
All the documents in this section have been carefully reviewed for content and accuracy. You can check out the Jupyter notebook associated with this documentation to see worked out examples of concepts covered in the text. Below you can find full documentation of the source files related to this notebook whose functions are exported by the package's module.
The Geometry section focuses on geometric interpretations and visualizations of linear algebra concepts, with Julia code examples. These examples are also found in the Julia code files documented below. There are several books that serve as source material for this section. I started with Linear Algebra and Geometry but found it a bit too elementary. I then moved on to Linear Algebra Through Geometry a book I had completely worked through in the past. Unfortunately I found it often too complicated and confusing. I finally settled on Practical Linear Algebra: A Geometry Toolbox which met my Goldilocks test.
This section is still a work in progress. Firstly, the current versions of the documents were generated by Claude from my notes on the book which I exported from Capacities. These have not yet been edited at all. I also have not yet covered all topics from the Geometry Toolbox book, particularly 3D geometry and more advanced material. Again, I plan to complete all these gaps in the future.
You can check out the Jupyter notebook associated with this documentation to see worked out examples of concepts covered in the text. Below you can find full documentation of the source files related to this notebook whose functions are exported by the package's module.
Finally, as I study more advanced Linear Algebra texts, I plan to add additional sections and code to this package.
Package's Module
The Linear Algebra package contains one module, Linear_Algebra.jl which essentially encapsulates and exports all aspects of code contained in this repository. The simplest way to access all its functionality is to use the Julia package manager to add the Github repository (which is open source). Then you can use the command using Linear_Algebra in your notebook, code or Julia REPL.
Basic Linear Algebra
The code for this can be found in the linear_algebra_basic.jl file.
Linear_Algebra.lin_ind_vec — Method
lin_ind_vec(vectors::Vector{<:Number}...)Test if the given vectors are linearly independent
Linear_Algebra.matrix_inverse — Method
matrix_inverse(A::Matrix)Test if the square matrix A is invertible and return its inverse.
A matrix is invertible if and only if $\det(A) \neq 0$, equivalently if $\operatorname{rank}(A) = n$ where n is the number of rows (and columns).
Returns inv(A) if invertible, or nothing if the matrix is singular.
Examples
A = [2.0 1.0; 1.0 -1.0]
matrix_inverse(A) # returns the 2×2 inverse
matrix_inverse([1.0 2.0; 2.0 4.0]) # singular — returns nothingLinear_Algebra.solve_linear_system — Method
solve_linear_system(A::Matrix, b::Vector)Diagnose and solve the linear system Ax = b.
Checks the rank of A against the augmented matrix [A b] to determine the nature of the system before solving:
- Inconsistent (no exact solution): returns the least-squares approximation via
pinv(A) * b(minimises ‖Ax - b‖); works for both square and non-square singular matrices. Prints the residual to confirm inexactness. - Underdetermined (infinite solutions): returns the minimum-norm solution via
pinv(A) * b(smallest ‖x‖ among all solutions). - Unique solution: returns the exact solution via
A \ b.
Using pinv for the non-unique cases avoids the SingularException that A \ b throws when A is square and singular.
Geometry Linear Algebra
The code for this can be found in the linear_algebra_geometry.jl file.
Linear_Algebra.barycentric_coord — Method
barycentric_coord(p::Point, q::Point, r::Point) -> t::Float64Calculate the barycentric co-ordinate or parater of points p and q with center of gravity point r
Linear_Algebra.calculate_param_line — Method
calculate_param_line(p::Point, q::Point, n::Int64) → [Point]Creates n points on a line defined by p and q, using the parametric equation of a line. Pure computational function - no plotting dependencies.
Linear_Algebra.center_of_gravity — Method
center of gravity(p::Point, q::Point, t) → rCreates center of gravity of points p and qusing parametric equation of a line
Linear_Algebra.distance_2_points — Method
distance_2_points(p::Point, q::Point) -> Norm(v)Distance between two points
Linear_Algebra.distance_to_implicit_line — Method
distancetoimplicit_line(a::Number, b::Number, c::Number, r::Point) -> Float64
Linear_Algebra.distance_to_parametric_line — Method
distancetoparametric_line(p::Point, v::Vector, r::Point) -> Float64 Definition of the line: l = p + tv
Linear_Algebra.distance_to_pnf_implicit_line — Method
distancetopnfimplicitline(â::RationalRoot, b̂::RationalRoot, ĉ::RationalRoot, r::Point) -> Float64
Linear_Algebra.explicit_line — Method
explicit_line(p::Point, q::Point) -> Tuple(Float64, Float64)The orthogonal vector α is calculated as: v = Vector(q -p) α = [v[2], -v[1]] The explicit equation of the line requires slope & intercept
Linear_Algebra.foot_of_line — Function
function footofline(P::Point, v::Vector, R::Point) -> Tuple(Point, Float64) Definition of the line: l = P + tv Return point Q on l closest to R Calculate distance from Q to A (foot of the line)
Linear_Algebra.foot_of_line — Function
function foot_of_line(A::Point, B::Point) -> Tuple(Point, Float64, Float64,Tuple(Point,Point,Point,Point))Definition of the line: l = P + tv where v is the vector from P to B Using original footofline and some additional calculations Return point on l closest to A == t[1] Return distance from t[1] to A (foot of the line) == t[2] Return area of parallelogram defined by A and B Return points on the parallelogram
Linear_Algebra.implicit_line_point_normal_form — Method
implicitlinepointnormalform(a::Number, b::Number, c::Number) -> Tuple(RationalRoot, RationalRoot, RationalRoot)
Linear_Algebra.implicit_to_parametric_line — Method
implicittoparametric line(a::Number, b::Number, c::Number) -> Tuple(Vector, Point)
Linear_Algebra.intersection_2_implicit_lines — Method
function intersection_2_implicit_lines(a₁::Number, b₁::Number, c₁::Number, a₂::Number, b₂::Number, c₂::Number) -> Vectorl₁: a₁x̂₁ + b₁x̂₂ + c₁ = 0 l₂: a₂x̂₁ + b₂x̂₂ + c₂ = 0 Solve for x̂₁ and x̂₂ which is the intersection point
Linear_Algebra.intersection_2_parametric_lines — Method
function intersection_2_parametric_lines(v::Vector, w::Vector, p::Point, q::Point) -> Vectorl₁ = p + tv l₂ = q + sw intersection: p + t̂v = q + ŝw The solution is a system of 2 equations in two unknowns expressed in equation: t̂v - ŝw = q - p
Linear_Algebra.is_orthogonal — Method
is_orthogonal(p::Point, q::Point) -> boolCheck if two vectors are is_orthogonal
Linear_Algebra.orthproj — Method
orthproj(v::Vector, w::Vector) -> VectorOrthogonol projection of vector w onto v
Linear_Algebra.parametric_to_implicit_line — Method
parametric_to_implicit_line(p::Point, v::Vector) -> Tuple{Number, Number, Number}The parametric equation is: l : l(t) = p + tv Use p and v to calculate: l : ax1 + bx2 + c = 0. And return co-efficient as tuple
Linear_Algebra.plot_param_line — Method
plot_param_line(p::Point, q::Point, n::Int64) → [Point]Creates n points on a line defined by p and q, using the parametric equation of a line, then plot
Linear_Algebra.point_in_implicit_line — Method
point_in_implicit_line(p::Point, q::Point, x::Point) -> Float64The orthogonal vector α is calculated as: v = Vector(q - p) α = [v[2], -v[1]] The implicit equation of the line is: α * (x -p) = 0 a = α[1] b = α[2] c = -(a * p[1]) - (b * p[2]) Then calculate the distance of point x from the line using the formula: (a * x[1] + b * x[2] + c) / norm([a, b] If 0 the point is in the line!
Linear_Algebra.polar_unit — Method
function polar_unit(y::Vector) -> Vector{Float64}Return unit vector in polar form for vector y
Linear_Algebra.rationalize — Method
function rationalize(x::Number; sigdigits=16) -> Rational(Number)Rationalize a number to a rational number
Linear_Algebra.reflection — Method
function reflection(v::Vector, w::Vector) -> VectorThe midpoint of the segment from v to the reflection of v around 'w', is the projection P from 'v' to the line along 'w'
Linear_Algebra.rotation — Method
function rotation(θ::Number, v::Vector) -> Vector θ : degrees to rotate v
Linear_Algebra.vector_angle_cos — Method
vector_angle_cos(p::Vector, q::Vector) -> cos θCalculate cosine of angle between 2 points
Linear Transformations
The code for this can be found in the linear_algebra_transform.jl file.
Linear_Algebra.projection_matrix — Method
function projection_matrix(x::Vector) -> Matrixvalue of Matrix with an actual vector x
Linear_Algebra.projection_matrix_polar — Method
function projection_matrix_polar(θ::Number) -> Matrixvalue of projection polar matrix with an actual value for the angle of the vector
Linear_Algebra.projection_matrix_symbolic — Method
function projection_matrix_symbolic() -> MatrixCreate symbolic matrix for projection on [u, v] using E₁ = [1,0] and E₂ =[0,1] to get first and second row
Linear_Algebra.projection_matrix_symbolic_polar — Method
function projection_matrix_symbolic_polar() -> MatrixCreate symbolic matrix for projection on [cos θ, sin θ]
Linear_Algebra.projection_matrix_transpose — Method
function projection_matrix_transpose(u::Vector) -> MatrixSee
Linear_Algebra.reflection_matrix — Method
function reflection_matrix(U::Vector) -> Matrixvalue of reflection Matrix with an actual vector U
Linear_Algebra.reflection_matrix_rational — Method
function reflection_matrix_rational(U::Vector) -> Matrixvalue of reflection Matrix with an actual vector U in rational form
Linear_Algebra.reflection_matrix_symbolic — Method
function reflection_matrix_symbolic() -> MatrixCreate symbolic matrix for reflection on [u, v] using E₁ = [1,0] and E₂ =[0,1] to get first and second colomn
Linear_Algebra.rotation_matrix — Method
function rotation_matrix(d::Number) -> Matrixvalue of rotation Matrix with an actual value for the rotation angle
Linear_Algebra.rotation_matrix_ns — Method
function rotation_matrix_ns(θ::Number) -> Matrixvalue of rotation Matrix with an actual value for the rotation angle - non symbolic version
Linear_Algebra.rotation_matrix_symbolic — Method
function rotation_matrix_symbolic() -> MatrixCreate symbolic matrix for rotation θ degrees, using E₁ = [1,0] and E₂ =[0,1] to get first and second column
Linear_Algebra.stretch_matrix — Method
function stretch_matrix_symbolic(n::Number) -> Matrixvalue of stretch Matrix with an actual value for the stretch factor
Linear_Algebra.stretch_matrix_symbolic — Method
function stretch_matrix_symbolic() -> MatrixCreate symbolic matrix for stretch λ₁, using E₁ = [1,0] and E₂ =[0,1] to get first and second column
LaTeX Display
The notebooks use LAlatex.jl (by ea42gh) together with BlockArrays.jl for clean LaTeX rendering of linear algebra objects. LAlatex provides display helpers for matrices, vectors, symbolic expressions, aligned derivations, piecewise functions, and block-partitioned matrices. These are declared in notebooks/Project.toml and loaded explicitly in the notebooks (using LAlatex, BlockArrays); they are not part of the Linear_Algebra module's public API.
See the LAlatex demo notebook in the original repository for a live tour of the main constructs, or run it interactively via the Binder demo.