updates to matrix.c

This commit is contained in:
Elizabeth Hunt 2023-10-13 21:09:27 -06:00
parent d21a7de801
commit 1b4d91e623
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
7 changed files with 94 additions and 87 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -373,49 +373,51 @@ void format_vector_into(Array_double *v, char *s) {
decomposed decomposed
#+BEGIN_SRC c #+BEGIN_SRC c
Matrix_double **lu_decomp(Matrix_double *m) { Matrix_double **lu_decomp(Matrix_double *m) {
assert(m->cols == m->rows); assert(m->cols == m->rows);
Matrix_double *u = copy_matrix(m); Matrix_double *u = copy_matrix(m);
Matrix_double *l = InitMatrixWithSize(double, m->rows, m->cols, 0.0); Matrix_double *l_empt = InitMatrixWithSize(double, m->rows, m->cols, 0.0);
put_identity_diagonal(l); Matrix_double *l = put_identity_diagonal(l_empt);
free(l_empt);
Matrix_double **u_l = malloc(sizeof(Matrix_double *) * 2);
for (size_t y = 0; y < m->rows; y++) { Matrix_double **u_l = malloc(sizeof(Matrix_double *) * 2);
if (u->data[y]->data[y] == 0) {
printf("ERROR: a pivot is zero in given matrix\n");
exit(-1);
}
}
if (u && l) { for (size_t y = 0; y < m->rows; y++) {
for (size_t x = 0; x < m->cols; x++) { if (u->data[y]->data[y] == 0) {
for (size_t y = x + 1; y < m->rows; y++) { printf("ERROR: a pivot is zero in given matrix\n");
double denom = u->data[x]->data[x]; exit(-1);
if (denom == 0) {
printf("ERROR: non-factorable matrix\n");
exit(-1);
}
double factor = -(u->data[y]->data[x] / denom);
Array_double *scaled = scale_v(u->data[x], factor);
Array_double *added = add_v(scaled, u->data[y]);
free_vector(scaled);
free_vector(u->data[y]);
u->data[y] = added;
l->data[y]->data[x] = -factor;
} }
} }
}
u_l[0] = u; if (u && l) {
u_l[1] = l; for (size_t x = 0; x < m->cols; x++) {
return u_l; for (size_t y = x + 1; y < m->rows; y++) {
} double denom = u->data[x]->data[x];
if (denom == 0) {
printf("ERROR: non-factorable matrix\n");
exit(-1);
}
double factor = -(u->data[y]->data[x] / denom);
Array_double *scaled = scale_v(u->data[x], factor);
Array_double *added = add_v(scaled, u->data[y]);
free_vector(scaled);
free_vector(u->data[y]);
u->data[y] = added;
l->data[y]->data[x] = -factor;
}
}
}
u_l[0] = u;
u_l[1] = l;
return u_l;
}
#+END_SRC #+END_SRC
*** ~bsubst~ *** ~bsubst~
+ Author: Elizabeth Hunt + Author: Elizabeth Hunt

Binary file not shown.

View File

@ -1,4 +1,4 @@
% Created 2023-10-13 Fri 20:48 % Created 2023-10-13 Fri 21:07
% Intended LaTeX compiler: pdflatex % Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article} \documentclass[11pt]{article}
\usepackage[utf8]{inputenc} \usepackage[utf8]{inputenc}
@ -31,22 +31,24 @@
\setlength\parindent{0pt} \setlength\parindent{0pt}
\section{Design} \section{Design}
\label{sec:org23cc15b} \label{sec:org400bcfc}
The LIZFCM static library is a successor to my attempt at writing codes for the The LIZFCM static library (at \href{https://github.com/Simponic/math-4610}{[https://github.com/Simponic/math-4610} is a successor to my
Fundamentals of Computational Mathematics course in Common Lisp, but the effort required attempt at writing codes for the Fundamentals of Computational Mathematics course in Common
to meet the requirement of creating a static library became too difficult to integrate Lisp, but the effort required to meet the requirement of creating a static library became
outside of the \texttt{ASDF} solution that Common Lisp already brings to the table. too difficult to integrate outside of the \texttt{ASDF} solution that Common Lisp already brings
to the table.
All of the work established in \texttt{deprecated-cl} has been painstakingly translated into All of the work established in \texttt{deprecated-cl} has been painstakingly translated into
the C programming language. I have a couple tenets for its design: the C programming language. I have a couple tenets for its design:
\begin{itemize} \begin{itemize}
\item Implemntations of routines should all be done immutably in respect to arguments. \item Implemntations of routines should all be done immutably in respect to arguments.
\item Functional programming is good (it's\ldots{} rough in this language though). \item Functional programming is good (it's\ldots{} rough in C though).
\item Routines are separated into "module" c files, and not individual files per function.
\end{itemize} \end{itemize}
\section{Compilation} \section{Compilation}
\label{sec:orgc704fb9} \label{sec:org050196a}
A provided \texttt{Makefile} is added for convencience. It has been tested on an M1 machine running MacOS as A provided \texttt{Makefile} is added for convencience. It has been tested on an M1 machine running MacOS as
well as Arch Linux. well as Arch Linux.
@ -72,11 +74,11 @@ Which is then bundled into a static library in \texttt{lib/lizfcm.a} which can b
in the standard method. in the standard method.
\section{The LIZFCM API} \section{The LIZFCM API}
\label{sec:org01b31c2} \label{sec:orgc36a923}
\subsection{Simple Routines} \subsection{Simple Routines}
\label{sec:org5355145} \label{sec:org3a0ee85}
\subsubsection{\texttt{smaceps}} \subsubsection{\texttt{smaceps}}
\label{sec:org4ed063f} \label{sec:org3d05353}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{smaceps} \item Name: \texttt{smaceps}
@ -102,7 +104,7 @@ float smaceps() {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{dmaceps}} \subsubsection{\texttt{dmaceps}}
\label{sec:orgcd0dfff} \label{sec:org2e7b4ce}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{dmaceps} \item Name: \texttt{dmaceps}
@ -128,9 +130,9 @@ double dmaceps() {
\end{verbatim} \end{verbatim}
\subsection{Derivative Routines} \subsection{Derivative Routines}
\label{sec:org8f54012} \label{sec:org2794f8b}
\subsubsection{\texttt{central\_derivative\_at}} \subsubsection{\texttt{central\_derivative\_at}}
\label{sec:org2c81fc1} \label{sec:org031253f}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{central\_derivative\_at} \item Name: \texttt{central\_derivative\_at}
@ -161,7 +163,7 @@ double central_derivative_at(double (*f)(double), double a, double h) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{forward\_derivative\_at}} \subsubsection{\texttt{forward\_derivative\_at}}
\label{sec:org149b09e} \label{sec:orgd674538}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{forward\_derivative\_at} \item Name: \texttt{forward\_derivative\_at}
@ -192,7 +194,7 @@ double forward_derivative_at(double (*f)(double), double a, double h) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{backward\_derivative\_at}} \subsubsection{\texttt{backward\_derivative\_at}}
\label{sec:orgbaa2238} \label{sec:orgd6eba18}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{backward\_derivative\_at} \item Name: \texttt{backward\_derivative\_at}
@ -223,9 +225,9 @@ double backward_derivative_at(double (*f)(double), double a, double h) {
\end{verbatim} \end{verbatim}
\subsection{Vector Routines} \subsection{Vector Routines}
\label{sec:orgf9dd708} \label{sec:orgc055fd6}
\subsubsection{Vector Arithmetic: \texttt{add\_v, minus\_v}} \subsubsection{Vector Arithmetic: \texttt{add\_v, minus\_v}}
\label{sec:orgbb91d9d} \label{sec:org3adc62d}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name(s): \texttt{add\_v}, \texttt{minus\_v} \item Name(s): \texttt{add\_v}, \texttt{minus\_v}
@ -256,7 +258,7 @@ Array_double *minus_v(Array_double *v1, Array_double *v2) {
\end{verbatim} \end{verbatim}
\subsubsection{Norms: \texttt{l1\_norm}, \texttt{l2\_norm}, \texttt{linf\_norm}} \subsubsection{Norms: \texttt{l1\_norm}, \texttt{l2\_norm}, \texttt{linf\_norm}}
\label{sec:org88bb4c5} \label{sec:org8064505}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name(s): \texttt{l1\_norm}, \texttt{l2\_norm}, \texttt{linf\_norm} \item Name(s): \texttt{l1\_norm}, \texttt{l2\_norm}, \texttt{linf\_norm}
@ -290,7 +292,7 @@ double linf_norm(Array_double *v) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{vector\_distance}} \subsubsection{\texttt{vector\_distance}}
\label{sec:org4499de6} \label{sec:org1c3d377}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{vector\_distance} \item Name: \texttt{vector\_distance}
@ -311,7 +313,7 @@ double vector_distance(Array_double *v1, Array_double *v2,
\end{verbatim} \end{verbatim}
\subsubsection{Distances: \texttt{l1\_distance}, \texttt{l2\_distance}, \texttt{linf\_distance}} \subsubsection{Distances: \texttt{l1\_distance}, \texttt{l2\_distance}, \texttt{linf\_distance}}
\label{sec:org1e61aea} \label{sec:orgb3ab95a}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name(s): \texttt{l1\_distance}, \texttt{l2\_distance}, \texttt{linf\_distance} \item Name(s): \texttt{l1\_distance}, \texttt{l2\_distance}, \texttt{linf\_distance}
@ -337,7 +339,7 @@ double linf_distance(Array_double *v1, Array_double *v2) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{sum\_v}} \subsubsection{\texttt{sum\_v}}
\label{sec:org57e2591} \label{sec:org26165af}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{sum\_v} \item Name: \texttt{sum\_v}
@ -357,7 +359,7 @@ double sum_v(Array_double *v) {
\subsubsection{\texttt{scale\_v}} \subsubsection{\texttt{scale\_v}}
\label{sec:org61b466a} \label{sec:orga88d1b9}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{scale\_v} \item Name: \texttt{scale\_v}
@ -376,7 +378,7 @@ Array_double *scale_v(Array_double *v, double m) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{free\_vector}} \subsubsection{\texttt{free\_vector}}
\label{sec:org398d778} \label{sec:org7caaac9}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{free\_vector} \item Name: \texttt{free\_vector}
@ -394,7 +396,7 @@ void free_vector(Array_double *v) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{copy\_vector}} \subsubsection{\texttt{copy\_vector}}
\label{sec:orgf6b116b} \label{sec:orgaa8259c}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{copy\_vector} \item Name: \texttt{copy\_vector}
@ -414,7 +416,7 @@ Array_double *copy_vector(Array_double *v) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{format\_vector\_into}} \subsubsection{\texttt{format\_vector\_into}}
\label{sec:org595519d} \label{sec:org9e52289}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{format\_vector\_into} \item Name: \texttt{format\_vector\_into}
@ -444,9 +446,9 @@ void format_vector_into(Array_double *v, char *s) {
\end{verbatim} \end{verbatim}
\subsection{Matrix Routines} \subsection{Matrix Routines}
\label{sec:org53505d6} \label{sec:orgb3fa0ab}
\subsubsection{\texttt{lu\_decomp}} \subsubsection{\texttt{lu\_decomp}}
\label{sec:org22ad28d} \label{sec:org796836c}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{lu\_decomp} \item Name: \texttt{lu\_decomp}
@ -464,8 +466,10 @@ Matrix_double **lu_decomp(Matrix_double *m) {
assert(m->cols == m->rows); assert(m->cols == m->rows);
Matrix_double *u = copy_matrix(m); Matrix_double *u = copy_matrix(m);
Matrix_double *l = InitMatrixWithSize(double, m->rows, m->cols, 0.0); Matrix_double *l_empt = InitMatrixWithSize(double, m->rows, m->cols, 0.0);
put_identity_diagonal(l); Matrix_double *l = put_identity_diagonal(l_empt);
free(l_empt);
Matrix_double **u_l = malloc(sizeof(Matrix_double *) * 2); Matrix_double **u_l = malloc(sizeof(Matrix_double *) * 2);
@ -505,7 +509,7 @@ Matrix_double **lu_decomp(Matrix_double *m) {
} }
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{bsubst}} \subsubsection{\texttt{bsubst}}
\label{sec:org15fec98} \label{sec:orgd15005d}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{bsubst} \item Name: \texttt{bsubst}
@ -530,7 +534,7 @@ Array_double *bsubst(Matrix_double *u, Array_double *b) {
} }
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{fsubst}} \subsubsection{\texttt{fsubst}}
\label{sec:orgdeab27c} \label{sec:org6beb581}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{fsubst} \item Name: \texttt{fsubst}
@ -558,7 +562,7 @@ Array_double *fsubst(Matrix_double *l, Array_double *b) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{solve\_matrix}} \subsubsection{\texttt{solve\_matrix}}
\label{sec:orge57c26b} \label{sec:org948d51a}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{src/matrix.c} \item Location: \texttt{src/matrix.c}
@ -594,7 +598,7 @@ Array_double *solve_matrix(Matrix_double *m, Array_double *b) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{m\_dot\_v}} \subsubsection{\texttt{m\_dot\_v}}
\label{sec:org6afa7d5} \label{sec:org41ef025}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{src/matrix.c} \item Location: \texttt{src/matrix.c}
@ -616,7 +620,7 @@ Array_double *m_dot_v(Matrix_double *m, Array_double *v) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{put\_identity\_diagonal}} \subsubsection{\texttt{put\_identity\_diagonal}}
\label{sec:orgdd1c373} \label{sec:org0d72ad5}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{src/matrix.c} \item Location: \texttt{src/matrix.c}
@ -635,7 +639,7 @@ Matrix_double *put_identity_diagonal(Matrix_double *m) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{copy\_matrix}} \subsubsection{\texttt{copy\_matrix}}
\label{sec:org3d1b7b0} \label{sec:org239b3f2}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{src/matrix.c} \item Location: \texttt{src/matrix.c}
@ -655,7 +659,7 @@ Matrix_double *copy_matrix(Matrix_double *m) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{free\_matrix}} \subsubsection{\texttt{free\_matrix}}
\label{sec:org697f6cc} \label{sec:org411f23a}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{src/matrix.c} \item Location: \texttt{src/matrix.c}
@ -674,7 +678,7 @@ void free_matrix(Matrix_double *m) {
\end{verbatim} \end{verbatim}
\subsubsection{\texttt{format\_matrix\_into}} \subsubsection{\texttt{format\_matrix\_into}}
\label{sec:orgc43bda3} \label{sec:org97f3a1a}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{format\_matrix\_into} \item Name: \texttt{format\_matrix\_into}
@ -701,9 +705,9 @@ void format_matrix_into(Matrix_double *m, char *s) {
} }
\end{verbatim} \end{verbatim}
\subsection{Linear Routines} \subsection{Linear Routines}
\label{sec:org1e850f2} \label{sec:org32fc5a8}
\subsubsection{\texttt{least\_squares\_lin\_reg}} \subsubsection{\texttt{least\_squares\_lin\_reg}}
\label{sec:org02e6d37} \label{sec:orgb604dc0}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Name: \texttt{least\_squares\_lin\_reg} \item Name: \texttt{least\_squares\_lin\_reg}
@ -733,12 +737,12 @@ Line *least_squares_lin_reg(Array_double *x, Array_double *y) {
} }
\end{verbatim} \end{verbatim}
\subsection{Appendix / Miscellaneous} \subsection{Appendix / Miscellaneous}
\label{sec:org83c0f8d} \label{sec:orgec2d061}
\subsubsection{Data Types} \subsubsection{Data Types}
\label{sec:org22f30f4} \label{sec:orgaae4ac1}
\begin{enumerate} \begin{enumerate}
\item \texttt{Line} \item \texttt{Line}
\label{sec:orgd014841} \label{sec:org802a412}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{inc/types.h} \item Location: \texttt{inc/types.h}
@ -751,7 +755,7 @@ typedef struct Line {
} Line; } Line;
\end{verbatim} \end{verbatim}
\item The \texttt{Array\_<type>} and \texttt{Matrix\_<type>} \item The \texttt{Array\_<type>} and \texttt{Matrix\_<type>}
\label{sec:org3f90e03} \label{sec:orgba5e93a}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{inc/types.h} \item Location: \texttt{inc/types.h}
@ -783,10 +787,10 @@ typedef struct {
\end{enumerate} \end{enumerate}
\subsubsection{Macros} \subsubsection{Macros}
\label{sec:org60b549e} \label{sec:org03e6970}
\begin{enumerate} \begin{enumerate}
\item \texttt{c\_max} and \texttt{c\_min} \item \texttt{c\_max} and \texttt{c\_min}
\label{sec:org04ff2db} \label{sec:orgee6bc6a}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{inc/macros.h} \item Location: \texttt{inc/macros.h}
@ -800,7 +804,7 @@ typedef struct {
\end{verbatim} \end{verbatim}
\item \texttt{InitArray} \item \texttt{InitArray}
\label{sec:orgf67f153} \label{sec:org00fb8ca}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{inc/macros.h} \item Location: \texttt{inc/macros.h}
@ -821,7 +825,7 @@ typedef struct {
\end{verbatim} \end{verbatim}
\item \texttt{InitArrayWithSize} \item \texttt{InitArrayWithSize}
\label{sec:org47e5e66} \label{sec:org5a66a1d}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{inc/macros.h} \item Location: \texttt{inc/macros.h}
@ -842,7 +846,7 @@ typedef struct {
\end{verbatim} \end{verbatim}
\item \texttt{InitMatrixWithSize} \item \texttt{InitMatrixWithSize}
\label{sec:org3b96b75} \label{sec:orgca67294}
\begin{itemize} \begin{itemize}
\item Author: Elizabeth Hunt \item Author: Elizabeth Hunt
\item Location: \texttt{inc/macros.h} \item Location: \texttt{inc/macros.h}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 213 KiB

After

Width:  |  Height:  |  Size: 215 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 142 KiB

After

Width:  |  Height:  |  Size: 160 KiB

View File

@ -35,8 +35,9 @@ Matrix_double **lu_decomp(Matrix_double *m) {
assert(m->cols == m->rows); assert(m->cols == m->rows);
Matrix_double *u = copy_matrix(m); Matrix_double *u = copy_matrix(m);
Matrix_double *l = InitMatrixWithSize(double, m->rows, m->cols, 0.0); Matrix_double *l_empt = InitMatrixWithSize(double, m->rows, m->cols, 0.0);
put_identity_diagonal(l); Matrix_double *l = put_identity_diagonal(l_empt);
free(l_empt);
Matrix_double **u_l = malloc(sizeof(Matrix_double *) * 2); Matrix_double **u_l = malloc(sizeof(Matrix_double *) * 2);