diff --git a/GaussJordan.py b/GaussJordan.py new file mode 100644 index 0000000..a4e369d --- /dev/null +++ b/GaussJordan.py @@ -0,0 +1,43 @@ +import numpy as np +# calculate the inverse of the matrix A using Gauss-Jordan algorithm +def GaussJordan (A): + (n, n) = A.shape + B = np.eye(n) + + # Assume A has an inverse matrix + success = 1 + + if np.linalg.det(A) == 0: + # A cannot be reversed + B = np.empty(n) + success = 0 + return + + # merge the two matrices + Ae = np.concatenate((A, B.T), axis=1) + for i in range (0, n): + if Ae[i, i] == 0: + B = np.empty(n) + success = 0 + return + # make the pivot position to one (as in the eye matrix) + Ae[i, :] = Ae[i, :] / Ae[i, i] + + #form zeros above and under the main diagonal in the + # first half and calculate the inverse in the second half + for j in range(0, n): + if i != j: + Ae[j, :] = Ae[j, :] - Ae[i, :] * Ae[j, i] + + #extract the inverse of matrix A from the augmented matrix + B = Ae[:, n : 2 * n ] + return (B, success) + + +# example for a 4x4 matrix +A = np.random.rand(4,4) +(inv, success) = GaussJordan(A) +print "The inverse using Gauss-Jordan is:" +print inv +print "The inverse using inv from linalg is:" +print np.linalg.inv(A) \ No newline at end of file diff --git a/matlab/ad-hoc/tests/Bezier.m b/matlab/ad-hoc/tests/Bezier.m new file mode 100644 index 0000000..57f43ba --- /dev/null +++ b/matlab/ad-hoc/tests/Bezier.m @@ -0,0 +1,43 @@ +## Copyright (C) 2018 Stefan +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} Bezier.py (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Stefan +## Created: 2018-10-22 + +function [] = Bezier(v, dim) + # result is a dim x 2 matrix + # result[i,1] - the x-coordinate + # result[i,2] - the y- coordinate + result = zeros(dim, 2); + i = 1; + # calculate the points from the Bezier curve with deCasteljau + # dim - the number of points from the Bezier curve + for u = linspace(0,1,dim) + result(i,:) = deCasteljau(v, length(v) - 1, 0, u); + i++; + endfor + + # plot the initials points and the Bezier Curve + plot (result(1:dim,1), result(1:dim,2), 'r','LineWidth',2) + hold + plot(v(:,1), v(:,2),'LineWidth',2) + title('Bezier Curve for given points') +endfunction \ No newline at end of file diff --git a/matlab/ad-hoc/tests/deCasteljau.m b/matlab/ad-hoc/tests/deCasteljau.m new file mode 100644 index 0000000..04181c6 --- /dev/null +++ b/matlab/ad-hoc/tests/deCasteljau.m @@ -0,0 +1,32 @@ +## Copyright (C) 2018 Stefan +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} deCasteljau (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Stefan +## Created: 2018-10-22 + +# Returns a point from the Bezier curve +function [result] = deCasteljau (v,i,j,t) + if i == 0 + result = v(j + 1, :); + else + result = (1 - t) * deCasteljau(v,i - 1,j,t) + t * deCasteljau(v,i - 1, j + 1,t); + endif +endfunction \ No newline at end of file diff --git a/matlab/ad-hoc/tests/testBezier.m b/matlab/ad-hoc/tests/testBezier.m new file mode 100644 index 0000000..b8e7d9b --- /dev/null +++ b/matlab/ad-hoc/tests/testBezier.m @@ -0,0 +1,37 @@ +# USES Bezier.m and DeCasteljau.m + +# Test 00 +# Bezier curve for n = 3 +figure +A = [1 1; 2 3; 4 3; 3 1] +Bezier(A, 250) + +# Test 01 +# Bezier curve for n = 3 +# Reason: 1st and 4th points are the same +figure +A = [1 1; 2 3; 4 3; 1 1]; +Bezier(A,250) + +# Test 02 +# Bezier curve for n = 3 +# Reason: Repeated 2 points +figure +A = [1 1; 2 3; 2 3; 3 1]; +Bezier(A,250) + +# Test 03 +# Bezier curve for n = 3 +# Reason: Modified the repeated points +figure +A = [1 1; 2 3; 2.2 3; 3 1]; +Bezier(A,250) + +# Test 04 +# Bezier curve for n = 3 +# Reason: One data point altered +figure +A = [1 1; 2 1; 4 3; 3 1] +Bezier (A, 250) + + diff --git a/python/Gershgorin.py b/python/Gershgorin.py new file mode 100644 index 0000000..3eb5bb8 --- /dev/null +++ b/python/Gershgorin.py @@ -0,0 +1,41 @@ +import numpy as np +import matplotlib.pyplot as plt + +def Gershgorin (A): + # A must be a square matrix + (m,n) = A.shape + if m != n: + print "You must introduce a square matrix" + return + + plt.axes() + # For each row: + for i in range (0, n): + # the circle has the center in (h, k) where + # h is the real part of A(i,i) and k is the real part of A(i,i) + h = np.real(A[i,i]) + k = np.imag(A[i,i]) + plt.plot(h, k, marker='x', markersize=5, color="blue") + + # the radius of the circle is the sum of + # norm of the elements in the row where i != j + + r = 0 + for j in range (0,n): + if i != j: + r = r + np.linalg.norm(A[i,j]) + # plot the circle + circle = plt.Circle((h, k), r, fill = False) + plt.gca().add_patch(circle) + + eigenval = np.linalg.eigvals(A) + # plot the eigenvalues of the matrix + for x in eigenval: + plt.plot(np.real(x), np.imag(x), marker='o', markersize=5, color="red") + + plt.axis('scaled') + plt.show() +# example for a 3x3 matrix +A = np.random.rand(3,3) +print A +Gershgorin(A) \ No newline at end of file