This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder