Compare commits

...

5 Commits

16 changed files with 648 additions and 5 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/test*
*.zip

View File

@ -15,8 +15,15 @@ cd workdir
echo "Generating modified Markdown file..."
# prefix every _ with an \
sed -i -e 's/_/\\_/g' "$NEWNAME"
#prefix every \ with a \
sed -i -e 's/\\\\ *$/\\\\\\\\/g' "$NEWNAME"
bat "$NEWNAME"
# prefix every individual _ with an \
sed -i -e 's/_([^_])/\\_\1/g' "$NEWNAME"
bat "$NEWNAME"
# remove tags section at the front
sed -i -e 's/tags:.*//g' "$NEWNAME"
@ -25,11 +32,32 @@ sed -i -e 's/---$//g' "$NEWNAME"
#remove markdown links
sed -i -e 's/\[.*\](.*)//g' "$NEWNAME"
# remove html tags
# remove code block lang declaration
sed -i -e 's/```.*/```/g' "$NEWNAME"
bat "$NEWNAME"
# add newlines around code block declaration
sed -i -e 's/``` *$/\n```\n/g' "$NEWNAME"
bat "$NEWNAME"
# replace special HTML tags
sed -i -e 's/<div id="page-break-after"\/>/\\pagebreak/g' "$NEWNAME"
sed -i -e 's/<br>/\\newline/g' "$NEWNAME"
# remove html tags
sed -i -e 's/<[a-zA-Z ]*>\(.*\)<\/[a-zA-Z =]*>/\1/g' "$NEWNAME"
sed -i -e 's/<.*\/>//g' "$NEWNAME"
sed -i -e 's/<.*>//g' "$NEWNAME"
# replace arrows
sed -i -e 's/->/$\\rightarrow$/g' "$NEWNAME"
sed -i -e 's/<-/$\\leftarrow$/g' "$NEWNAME"
sed -i -e 's/=>/$\\Rightarrow$/g' "$NEWNAME"
sed -i -e 's/<=/$\\Leftarrow$/g' "$NEWNAME"
bat "$NEWNAME"
echo "Generating LaTeX file..."
echo "\documentclass[a4paper, 11pt]{article}
@ -46,5 +74,6 @@ tlmgr install markdown
echo "Building PDF..."
pdflatex -synctex=1 -interaction=nonstopmode --shell-escape file.tex
echo "Done."
okular file.pdf
cp *.pdf ..
echo "Done."

17
w1/matlab/convert2basis.m Normal file
View File

@ -0,0 +1,17 @@
% EX2 a)
function a = convert2basis(n, b)
if (b < 2)
disp('basis needs to be at least 2!')
return
end
v = [];
while n > 0
v = [mod(n, b), v];
n = floor(n / b);
end
a = v
end

67
w1/matlab/flp.m Normal file
View File

@ -0,0 +1,67 @@
% EX3 c)
function [d, v, t] = flp(b, m, n, x)
% GETS:
% b: basis
% m: mantissa length
% n: exponent length
% x: number to convert
% RETURNS:
% d: mantissa coefficients
% v: coefficients of the exponent
% t: sign
# calculate exponent and mantissa
exponent = 0
mantissa = x
while mantissa > 1
mantissa /= b
exponent += 1
end
while (mantissa * b) < 1
mantissa *= b
exponent -= 1
end
inverted = convert2basis(mantissa^-1, b)
inverted(end)=[]
val_man = flip(inverted)
val_exp = convert2basis(abs(exponent), b)
t = sign(exponent)
# pad result
end
function a = convert2basis(n, b)
if (b < 2)
disp('basis needs to be at least 2!')
return
end
v = [];
while n > 0
v = [mod(n, b), v];
n = floor(n / b);
end
a = v
end
function b = convertDecimal(n, b)
if (b < 2)
disp('basis needs to be at least 2!')
return
end
while n < 1
end
function res = padArr(arr, len)
for i = length(arr):len
arr(i+1) = 0
end
res = arr
end

21
w1/matlab/interval.practise.m Executable file
View File

@ -0,0 +1,21 @@
function interval(x, a, b)
if (nargin < 2)
a = 7;
b = 12;
end
if (a > b)
bNew = a
a = b
b = bNew
end
if (a <= x && x <= b)
disp('x ist im Intervall')
elseif (x < a)
disp('x ist unterhalb des Intervals')
elseif (x > b)
disp('x ist überhalb des Intervals')
else
disp('x ist keine Nummer')
end
end

View File

@ -0,0 +1,91 @@
% Angewandte Numerik 1, SoSe 2022
% Uebungsblatt 01, Aufgabe 02: Darstellung natuerlicher Zahlen
%
% Testprogramm fuer die Funktion a = convert2basis(n, b)
%
% Letzte Aenderung: 22.04.2022
%% Initialisierung
clearvars;
close all;
clc;
fprintf('\n');
fprintf('Angewandte Numerik 1, Sommersemester 2022\n');
fprintf('Uebungsblatt 1, Aufgabe 02: Darstellung natuerlicher Zahlen\n');
fprintf('\n');
%% Definition und Durchfuehrung der Testfaelle
testfall = 0;
while true % alle Testfaelle untersuchen
testfall = testfall + 1; % naechster Testfall
%% alle Testfaelle definieren
switch testfall
case 1 % Testfall 1: b = 2, n = 30
b = 2;
n = 30;
a = [1 1 1 1 0];
case 2 % Testfall 2: b = 2, n = 31
b = 2;
n = 31;
a = [1 1 1 1 1];
case 3 % Testfall 3: b = 2, n = 32
b = 2;
n = 32;
a = [1 0 0 0 0 0];
case 4 % Testfall 4: b = 2, n = 33
b = 2;
n = 33;
a = [1 0 0 0 0 1];
case 5 % Testfall 5: b = 2, n = 42
b = 2;
n = 42;
a = [1 0 1 0 1 0];
case 6 % Testfall 6: b = 2, n = 134110
b = 2;
n = 134110;
a = [1 0 0 0 0 0 1 0 1 1 1 1 0 1 1 1 1 0];
case 7 % Testfall 7: b = 8, n = 27
b = 8;
n = 27;
a = [3 3];
case 8 % Testfall 8: b = 8, n = 3652
b = 8;
n = 3652;
a = [7 1 0 4];
case 9 % Testfall 9: b = 8, n = 46807
b = 8;
n = 46807;
a = [1 3 3 3 2 7];
case 10 % Testfall 10: b = 10, n = 3121
b = 10;
n = 3121;
a = [3 1 2 1];
case 11 % Testfall 11: b = 10, n = 192310030133
b = 10;
n = 192310030133;
a = [1 9 2 3 1 0 0 3 0 1 3 3];
otherwise
break; % keine Testfaelle mehr vorhanden
end
%% Testfall durchfuehren und Ergebnis ausgeben
spezString = 'Testfall %2d (b = %2d, n = %12d): %s.\n';
if max(abs(a - convert2basis(n, b))) == 0
fprintf(spezString, testfall, b, n, 'Bestanden');
else
fprintf(2, spezString, testfall, b, n, 'Fehlgeschlagen')
end
end

105
w1/matlab/testFlp.m Normal file
View File

@ -0,0 +1,105 @@
% Angewandte Numerik 1, SoSe 2022
% Uebungsblatt 01, Aufgabe 03: Wert einer Gleitpunktdarstellung
%
% Testprogramm fuer die Funktion [d, v, t] = flp(b, m, n, x)
%
% Letzte Aenderung: 22.04.2022
%% Initialisierung
clearvars;
close all;
clc;
fprintf('\n');
fprintf('Angewandte Numerik 1, Sommersemester 2022\n');
fprintf('Uebungsblatt 1, Aufgabe 3d: Gleitpunkt-Darstellung\n');
fprintf('\n');
%% Definition und Durchfuehrung der Testfaelle
testfall = 0;
while true % alle Testfaelle untersuchen
testfall = testfall + 1; % naechster Testfall
%% alle Testfaelle definieren
switch testfall
case 1 % Testfall 1
b = 2; % Basis
m = 8; % Laenge der Mantisse
n = 3; % Laenge des Exponenten
x = 27.375; % zu konvertierende Zahl
dSoll = [1 1 0 1 1 0 1 1]; % Mantisse
vSoll = [1 0 1]; % Exponent
tSoll = 1; % Vorzeichen Exponent
case 2 % Testfall 2
b = 8; % Basis
m = 8; % Laenge der Mantisse
n = 3; % Laenge des Exponenten
x = 27.375; % zu konvertierende Zahl
dSoll = [3 3 3 0 0 0 0 0]; % Mantisse
vSoll = [0 0 2]; % Exponent
tSoll = 1; % Vorzeichen Exponent
case 3 % Testfall 3
b = 2; % Basis
m = 10; % Laenge der Mantisse
n = 3; % Laenge des Exponenten
x = 9.140625; % zu konvertierende Zahl
dSoll = [1 0 0 1 0 0 1 0 0 1]; % Mantisse
vSoll = [1 0 0]; % Exponent
tSoll = 1; % Vorzeichen Exponent
case 4 % Testfall 4
b = 8; % Basis
m = 5; % Laenge der Mantisse
n = 3; % Laenge des Exponenten
x = 9.140625; % zu konvertierende Zahl
dSoll = [1 1 1 1 0]; % Mantisse
vSoll = [0 0 2]; % Exponent
tSoll = 1; % Vorzeichen Exponent
case 5 % Testfall 5
b = 2; % Basis
m = 3; % Laenge der Mantisse
n = 2; % Laenge des Exponenten
x = 0.375; % zu konvertierende Zahl
dSoll = [1 1 0]; % Mantisse
vSoll = [0 1]; % Exponent
tSoll = -1; % Vorzeichen Exponent
case 6 % Testfall 6
b = 8; % Basis
m = 5; % Laenge der Mantisse
n = 2; % Laenge des Exponenten
x = 0.0157470703125; % zu konvertierende Zahl
dSoll = [1 0 0 4 0]; % Mantisse
vSoll = [0 1]; % Exponent
tSoll = -1; % Vorzeichen Exponent
case 7 % Testfall 7
b = 2; % Basis
m = 3; % Laenge der Mantisse
n = 3; % Laenge des Exponenten
x = 0.0625; % zu konvertierende Zahl
dSoll = [1 0 0]; % Mantisse
vSoll = [0 1 1]; % Exponent
tSoll = -1; % Vorzeichen Exponent
otherwise
break; % keine Testfaelle mehr vorhanden
end
%% Testfall durchfuehren und Ergebnis ausgeben
[d, v, t] = flp(b, m, n, x);
spezString = 'Testfall %d: %s.\n';
if max(abs([d - dSoll, v - vSoll, t - tSoll])) == 0
fprintf(spezString, testfall, 'Bestanden');
else
fprintf(2, spezString, testfall, 'Fehlgeschlagen');
end
end

67
w1/matlab/testValue.m Normal file
View File

@ -0,0 +1,67 @@
% Angewandte Numerik 1, SoSe 2022
% Uebungsblatt 01, Aufgabe 03: Wert einer Gleitpunktdarstellung
%
% Testprogramm fuer die Funktion x = value(b, d, v, t)
%
% Letzte Aenderung: 22.04.2022
%% Initialisierung
clearvars;
close all;
clc;
tol = 1e-14; % Geforderte Genauigkeit der Berechnungen
fprintf('\n');
fprintf('Angewandte Numerik 1, Sommersemester 2022\n');
fprintf('Uebungsblatt 1, Aufgabe 3b: Gleitpunkt-Darstellung\n');
fprintf('\n');
%% Definition und Durchfuehrung der Testfaelle
testfall = 0;
while true % alle Testfaelle untersuchen
testfall = testfall + 1; % naechster Testfall
%% alle Testfaelle definieren
switch testfall
case 1 % Testfall 1
b = 2; % Basis
d = [1 0 1 0 0 1 0 0]; % Mantisse
v = [0 0 1 0 0]; % Exponent
t = 1; % Vorzeichen Exponent
x = 10.25; % Wert der Zahl
case 2 % Testfall 2
b = 2; % Basis
d = [1 0 1]; % Mantisse
v = [0 0 1]; % Exponent
t = -1; % Vorzeichen Exponent
x = 0.3125; % Wert der Zahl
case 3 % Testfall 3
b = 8; % Basis
d = [7 5 0]; % Mantisse
v = [0 0 1]; % Exponent
t = -1; % Vorzeichen Exponent
x = 0.119140625; % Wert der Zahl
otherwise
break; % keine Testfaelle mehr vorhanden
end
%% Testfall durchfuehren und Ergebnis ausgeben
spezString = 'Testfall %d: %s.\n';
if abs(x - value(b,d,v,t)) < tol
fprintf(spezString, testfall, 'Bestanden');
else
fprintf(2, spezString, testfall, 'Fehlgeschlagen');
end
end

38
w1/matlab/value.m Normal file
View File

@ -0,0 +1,38 @@
% EX3 a)
function x = value(b, d, v, t)
% b: basis
% d: mantissa coefficients
% v: coefficients of the exponent
% t: sign
if b < 2
disp('basis needs to be at least 2!')
return
elseif !(t == -1 || t == 1)
disp('t needs to be 1 or -1!')
return
end
exponent = (t * unConvert(v, b))
prefactor = b ^ exponent
mantissa = unConvertMantissa(d, b)
x = prefactor * mantissa
end
function val = unConvert(n, b)
nums = flip(n)
res = 0
for i = 1:length(n)
res += nums(i) * b^(i-1)
end
val = res
end
function val = unConvertMantissa(m, b)
res = 0
for i = 1:length(m)
res += m(i) * b^(-i)
end
val = res
end

32
w3/gaussLR.m Normal file
View File

@ -0,0 +1,32 @@
function A = gaussLR(A)
% Gaussian Elimination for reforming the matrix into left-lower and right-upper triangular matrix
% A: A R^(n \times n) matrix
% actually not used here (but it was planned to be used): bsxfun, which makes this possible: bsxfun(@times, [1,2;3,4], [1;2]) = [1,2;6,8]. It multiplies the first row by 1, and the second one by 2.
% newer (and actually (still not) used here): [1,2;3,4].*[1;2] = [1,2;6,8]
% for convenience
len = length(A)
for i = 1:len
prefactor = A(i,i)
if prefactor == 0
error("LR-Zerlegung failed. Please use pivotization, or check that the matrix is regular!")
end
% elimination (find R)
factors = zeros(len, 1);
for j = (i+1):len
factors(j) = A(j, i) / prefactor;
end
% A(:,i:len) describes the matrix A with only the last i cols (so, for example, a 3x3 matrix with (:,2,len) with len=3) is the last 2 cols of the matrix, and with that a 2x3 matrix
% extra step for traceability, could also be compressed to a single line
multmatrix = (-factors * A(i,i:len))
A(:,i:len) = A(:,i:len) + multmatrix;
% save factors for L
A(:,i) = A(:,i) + factors
end
end

47
w3/lrPivot.m Normal file
View File

@ -0,0 +1,47 @@
function [A, P] = lrPivot(A)
% Gaussian Elimination for reforming the matrix into left-lower and right-upper triangular matrix
% A: A R^(n \times n) matrix
% P: Permutations-Matrix
% actually not used here (but it was planned to be used): bsxfun, which makes this possible: bsxfun(@times, [1,2;3,4], [1;2]) = [1,2;6,8]. It multiplies the first row by 1, and the second one by 2.
% newer (and actually (still not) used here): [1,2;3,4].*[1;2] = [1,2;6,8]
% for convenience
len = length(A)
pvec = 1:len
% Generate LR-Zerlegung
for i = 1:len
% pivot
[maxVal, maxValPos] = max(abs(A(pvec(i:end), i)))
% swap
temp = pvec(i)
pvec(i) = pvec(maxValPos + i - 1)
pvec(maxValPos + i - 1) = temp
prefactor = A(pvec(i),i)
if prefactor == 0
error("LR-Zerlegung failed. Please check that the matrix is regular!")
end
% elimination (find R)
factors = zeros(len, 1)
for j = (i+1):len
factors(pvec(j)) = A(pvec(j), i) / prefactor
end
% A(:,i:len) describes the matrix A with only the last i cols (so, for example, a 3x3 matrix with (:,2,len) with len=3) is the last 2 cols of the matrix, and with that a 2x3 matrix
% extra step for traceability, could also be compressed to a single line
multmatrix = (-factors * A(pvec(i),i:len))
A(:,i:len) = A(:,i:len) + multmatrix
% save factors for L
A(:,i) = A(:,i) + factors
end
P = eye(len)(:,pvec)
end

30
w3/solveLR.m Normal file
View File

@ -0,0 +1,30 @@
function x_ret = solveLR(A, b)
len = length(b)
z = zeros(len,1)
% solve Lz = b
for i = 1:len
z(i) = b(i);
for j = 1:(i-1)
z(i) = z(i) - z(j) * A(i, j);
end
% no division necessary as L is unipotent
end
x = zeros(len,1)
% solve Rx = z
for i = len:-1:1
x(i) = z(i);
for j = len:-1:(i+1)
x(i) = x(i) - x(j) * A(i, j);
end
x(i) = x(i) / A(i, i)
end
x_ret = x
end

30
w3/solveLrPivot.m Normal file
View File

@ -0,0 +1,30 @@
function x = solveLrPivot(A, P, b)
len = length(b)
z = zeros(len,1)
% solve Lz = b
for i = 1:len
z(i) = b(i)
for j = 1:(i-1)
z(i) = z(i) - z(j) * A(P(i), j)
end
% no division necessary as L is unipotent
end
x = zeros(len,1)
% solve Rx = z
for i = len:-1:1
x(i) = z(i)
for j = len:-1:(i+1)
x(i) = x(i) - x(j) * A(P(i), j)
end
x(i) = x(i) / A(P(i), i)
end
x_ret = x
end

14
w3/testGaussLR.m Normal file
View File

@ -0,0 +1,14 @@
function testGaussLR()
success = testSingle( [ 1,1,1; 4,3,-1; 3,5,3 ], [1,1,1; 4,-1,-5; 3,-2,-10] )
success = success & testSingle( [1,0;0,1], [1,0;0,1] )
success = success & testSingle( [6,-4,7; -12,5,-12; 18,0,22], [6,-4,7; -2,-3,2; 3,-4,9] )
if success
disp("It works!")
else
disp("It doesn't work :/")
end
end
function success = testSingle(init, expected)
success = isequal(gaussLR(init), expected);
end

40
w3/testSolve.m Normal file
View File

@ -0,0 +1,40 @@
function testSolve
result = Test([0, 1; 1, 1], [1; 1])
result = result & Test([11,44,1; 0.1,0.4,3; 0,1,-1], [1; 1; 1])
result = result & Test([0.001,1,1; -1,0.004,0.004; -1000,0.004,0.000004], [1; 1; 1])
if result(1) % nopivot
disp("Pivot-less Solving worked!")
else
disp("Pivot-less Solving broke!")
end
if result(2) % pivot
disp("Pivot-full Solving worked!")
else
disp("Pivot-full Solving broke!")
end
end
function [nopivot, pivot] = Test(A, b)
try
nopivot = isequal(Solve(A, b), linsolve(A, b))
catch
nopivot = false
end
try
pivot = isequal(SolvePivot(A, b), linsolve(A, b))
catch
pivot = false
end
end
function x = Solve(A, b)
x = solveLR(gaussLR(A), b)
end
function x = SolvePivot(A, b)
[A, T] = lrPivot(A)
x = solveLrPivot(A, T, b)
end

14
w3/testSolveLR.m Normal file
View File

@ -0,0 +1,14 @@
function testSolveLR()
success = test( [1,3,2; 2,15,2; 1,3,4], [1;2;3] )
success = success & test ( [1,1,1; 4,3,-1; 3,5,3], [4;2;0] )
success = success & test ( [1, 0, 0; 0, 1, 0; 0, 0, 1], [4; 2; 0] )
if success
disp("It works!")
else
disp("It broke!")
end
end
function success = test(A, b)
success = isequal(linsolve(A, b), solveLR(gaussLR(A), b))
end