Skip to content

Instantly share code, notes, and snippets.

View ivankahl's full-sized avatar
🤪
"Maybe if I close and open it again it'll build..."

Ivan Kahl ivankahl

🤪
"Maybe if I close and open it again it'll build..."
View GitHub Profile
@ivankahl
ivankahl / determinant.py
Created August 17, 2020 07:04
Python 3 function that can be used to calculate the determinant of any n x n matrix
def det(m):
"""Returns the determinant for the n x n matrix m"""
# If the matrix is 2x2, then we just calculate the determinant
# simply otherwise we will use Laplace Expansion
if len(m) == 2:
return (m[0][0] * m[1][1]) - (m[1][0] * m[0][1])
else:
d = 0
for i in range(len(m)):
new_m = [x[0:i] + x[i+1:] for x in m[1:]]
@ivankahl
ivankahl / iterative_linear_solve.m
Created May 15, 2019 20:39
An Octave method that takes an iterative method and executes it repeatedly until an acceptable solution for a linear system of equations is obtained.
function xnew = iterative_linear_solve(A, b, xinitial, tolerance, max_iterations, method)
% This method will iteratively execute a method (e.g. Jacobi, Gauss-Seidel)
% to solve a linear system. It will stop when a solution has been reached
% whose relative error is smaller than the tolerance or when the maximum
% number of iterations have been exceeded
% We will need to keep the previous x-values after we have calculated
% the new ones.
xold = xinitial;
% We also want to count our iterations to make sure we don't exceed the
@ivankahl
ivankahl / jacobi.m
Last active April 3, 2024 20:55
The Jacobi method implemented in Octave.
function xnew = jacobi(A, b, xold)
% This is a sample implementation of the Jacobi method in
% Octave.
% We first need to determine how many equations there are
% that we need to solve
n = size(A, 1);
% We create a blank xnew vector to store the final results
xnew = zeros(n, 1);
@ivankahl
ivankahl / gauss_seidel.m
Last active August 22, 2021 19:23
The Gauss-Seidel method implemented in Octave.
function xnew = gauss_seidel(A, b, xold)
% This implementation of the Gauss-Seidel method in Octave uses
% a single loop. It is based on the solution provided in the
% APM1513 course work from UNISA.
% We first need to determine how many equations we need to solve
n = size(A, 1);
% We set the xnew to have the xold values
xnew = xold;
@ivankahl
ivankahl / power_method.m
Created May 5, 2019 15:36
The Power Method written in Octave that can be used to find the dominant Eigenvalue and Eigenvector of a matrix iteratively.
function [eigen_vector, eigen_value] = power_method(A, tolerance, max_iter)
k = 0;
n = size(A, 1);
eigen_vector_old = rand(n, 1);
do
% Calculate a new approximation for the dominant eigen vector
eigen_vector_new = A * eigen_vector_old;
% Calculate a new approximate dominant eigen value
@ivankahl
ivankahl / selectionSort.py
Created May 7, 2017 10:43
selectionSort.py
def selection_sort(arr):
for i in range(len(arr) - 1):
for j in range(i + 1, len(arr)):
if arr[j] < arr[i]:
arr[j], arr[i] = arr[i], arr[j]
return arr
@ivankahl
ivankahl / bubbleSort.py
Created May 7, 2017 10:42
bubbleSort.py
def bubble_sort(arr):
i = len(arr)
sorted = False
while(not sorted):
sorted = True
for j in range(0, i - 1):
if arr[j + 1] < arr[j]:
static int LinearSearchWithFlag(int[] items, int itemToSearch)
{
// Initialize our counter (i) to 0 (the position of the first
// element)
int i = 0;
// Loop through the array until we reach the end or until the
// the item at the current index matches the item we are looking
// for
while (i < items.Length && items[i] != itemToSearch)
// Needs the array to already be SORTED!
static int BinarySearch(int[] items, int itemToSearch)
{
// Create a variable to store the lower boundary
int start = 0,
// Create a variable to store the upper boundary
end = items.Length - 1,
// Create a variable to store the middle of the boundary
mid = (start + end) / 2;
// Create a variable to determine whether the item is
static int[] RemoveDuplicates(int[] items)
{
// Create a new array to store the items with no
// duplicates
List<int> newList = new List<int> { items[0] };
// Loop through every item in our items array
for (int i = 1; i < items.Length; i++)
{
// Create a new boolean to determine whether