Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test bezier #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Bezier.m
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{retval} =} Bezier.py (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn

## Author: Stefan <stefan@Specter>
## 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
43 changes: 43 additions & 0 deletions GaussJordan.py
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions deCasteljau.m
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{retval} =} deCasteljau (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn

## Author: Stefan <stefan@Specter>
## 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
43 changes: 43 additions & 0 deletions matlab/ad-hoc/tests/Bezier.m
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{retval} =} Bezier.py (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn

## Author: Stefan <stefan@Specter>
## 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
32 changes: 32 additions & 0 deletions matlab/ad-hoc/tests/deCasteljau.m
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {@var{retval} =} deCasteljau (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn

## Author: Stefan <stefan@Specter>
## 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
37 changes: 37 additions & 0 deletions matlab/ad-hoc/tests/testBezier.m
Original file line number Diff line number Diff line change
@@ -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)


41 changes: 41 additions & 0 deletions python/Gershgorin.py
Original file line number Diff line number Diff line change
@@ -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)
37 changes: 37 additions & 0 deletions testBezier.m
Original file line number Diff line number Diff line change
@@ -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)