feat: halley's method + pdf -> img

This commit is contained in:
newt 2024-10-09 18:02:46 +01:00
parent e032ee5d51
commit 11c3e90c57
25 changed files with 120 additions and 1382 deletions

View file

@ -2,5 +2,5 @@
. "$(dirname "$0")/_/husky.sh"
npx pretty-quick --staged
node .husky/scripts/cleanMaths.js
.husky/scripts/pdfToPng.sh
git add .

View file

@ -1,73 +0,0 @@
const fs = require('fs');
const path = require('path');
const { pdfToPng } = require('pdf-to-png-converter');
const Jimp = require('jimp');
const mathsDir = path.resolve(__dirname, '..', '..', 'maths');
const cropBuffer = buffer =>
new Promise((resolve, reject) => {
Jimp.read(buffer)
.then(cropped => {
cropped.autocrop();
new Jimp(
cropped.getWidth() + 100,
cropped.getHeight() + 100,
'#ffffff',
async (err, image) => {
if (err) reject(err);
image.blit(cropped, 50, 50);
resolve(await image.getBufferAsync(Jimp.MIME_PNG));
}
);
})
.catch(err => reject(err));
});
const crawlDirectory = async dir => {
const dirents = fs.readdirSync(dir, { withFileTypes: true });
const files = await Promise.all(
dirents.map(dirent => {
const res = path.resolve(dir, dirent.name);
return dirent.isDirectory() ? crawlDirectory(res) : res;
})
);
return files.flat();
};
(async () => {
const files = await crawlDirectory(mathsDir);
files.forEach(file => {
const [filePath, fileDirectory, fileNameWithExt] = file.match(/(.*)\/((?:.(?!\/))+)$/);
const [fileName, fileExtension] = fileNameWithExt.split('.');
if (file.endsWith('.pdf')) {
pdfToPng(filePath).then(async output => {
const pageCount = output.length;
if (pageCount > 1)
output.forEach(async (page, i) =>
fs.writeFileSync(
path.join(fileDirectory, `${fileName}-${i + 1}.png`),
await cropBuffer(page.content)
)
);
else
fs.writeFileSync(
path.join(fileDirectory, `${fileName}.png`),
await cropBuffer(output[0].content)
);
});
}
if (fileExtension !== 'tex' && fileExtension !== 'png' && fileExtension !== 'cls') {
fs.rmSync(filePath);
}
});
})();

22
.husky/scripts/pdfToPng.sh Executable file
View file

@ -0,0 +1,22 @@
#!/bin/bash
find . -print0 | while IFS= read -r -d '' file
do
if [ -f "$file" ]; then
if [[ $file == *pdf ]]; then
mkdir temp
gm convert "$file" +adjoin temp/temp%02d.png
for temp in ./temp/*.png
do
gm convert "$temp" -fuzz 80% -trim +repage -bordercolor white -border 50x25 "$temp"
done
readarray -d . -t arr <<< $file
gm convert -append ./temp/*.png "${arr[1]:1}.png"
rm -rf temp
rm "$file" "${arr[1]:1}.synctex.gz"
fi
fi
done

BIN
maths/halley's method.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

53
maths/halley's method.tex Normal file
View file

@ -0,0 +1,53 @@
\documentclass{style}
\usepackage{amsmath}
\usepackage{breqn}
\usepackage{mathtools}
\begin{document}
\begin{center}
Suppose we have an $n$th degree polynomial, such that $f(x) = c_{n}x^{n} + c_{n-1}x^{n-1} + c_{n-2}x^{n-2} + ... + c_{0} = \sum_{i=0}^{n}c_{n-i}x^{n-i}$
\hfill
By the power rule of differentiation, we can conclude that the first derivative of $f(x)$ is as follows
\begin{dmath*}
f'(x) = nc_{n}x^{n-1} + (n-1)c_{n-1}x^{n-2} + (n-2)c_{n-2}x^{n-3} + ... + c_{1} = \sum_{i=0}^{n-1}(n-i)c_{n-i}x^{n-i-1}
\end{dmath*}
And the second derivative is
\begin{dmath*}
f''(x) = n(n-1)c_{n}x^{n-2} + (n-1)(n-2)c_{n-1}x^{n-3} + (n-2)(n-3)c_{n-2}x^{n-4} + ...+ c_{2} = \sum_{i=0}^{n-2}(i-n)(i-n+1)a_{n-i}x^{n-i-2}
\end{dmath*}
After starting with an initial guess, the next iteration of Halley's method is given by $x_k - \frac{2f(x_k)f'(x_k)}{2[f'(x_k)]^2 - f(x_k)f''(x_k)}$.
This means that we must first find $f(x)f'(x)$, $[f'(x)]^2$, and $f(x)f''(x)$. These all consist of the multiplication of two series - there is a nice general form to this problem stated below
\begin{dmath*}
(\sum_{i=0}^{n}x_{i})(\sum_{j=0}^{m}y_{j}) = \sum_{i=0}^{n}\sum_{j=0}^{m}x_iy_j
\end{dmath*}
\newpage
From this, we can conclude that:
\begin{dmath*}
f(x)f'(x) = (\sum_{i=0}^{n}c_{n-i}x^{n-i})(\sum_{i=0}^{n-1}(n-i)c_{n-i}x^{n-i-1}) = \sum_{i=0}^{n}\sum_{j=0}^{n-1}(n-j)c_{n-i}c_{n-j}x^{2n-i-j-1}
\end{dmath*}
\begin{dmath*}
[f'(x)]^2 = (\sum_{i=0}^{n-1}(n-i)c_{n-i}x^{n-i-1})(\sum_{i=0}^{n-1}(n-i)c_{n-i}x^{n-i-1}) = \sum_{i=0}^{n-1}\sum_{j=0}^{n-1}(n-i)(n-j)c_{n-i}c_{n-j}x^{2(n-1)-i-j}
\end{dmath*}
\begin{dmath*}
f(x)f''(x) = (\sum_{i=0}^{n}c_{n-i}x^{n-i})(\sum_{i=0}^{n-2}(i-n)(i-n+1)a_{n-i}x^{n-i-2}) = \sum_{i=0}^{n}\sum_{j=0}^{n-2}(j-n)(j-n+1)c_{n-i}c_{n-j}x^{2(n-1)-i-j}
\end{dmath*}
And all that is left to do is to plug it into the formula for Halley's method, leaving us with the following:
\begin{align*}
x_{k+1} = x_{k} - \dfrac{2\sum_{i=0}^{n}\sum_{j=0}^{n-1}(n-j)c_{n-i}c_{n-j}x^{2n-i-j-1}}{\splitdfrac{2\sum_{i=0}^{n-1}\sum_{j=0}^{n-1}(n-i)(n-j)c_{n-i}c_{n-j}x^{2(n-1)-i-j}}{- \sum_{i=0}^{n}\sum_{j=0}^{n-2}(j-n)(j-n+1)c_{n-i}c_{n-j}x^{2(n-1)-i-j}}}
\end{align*}
\end{center}
\end{document}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

@ -1,6 +1,9 @@
\LoadClass[17pt]{extarticle}
\pagenumbering{gobble}
\usepackage{geometry}
\geometry{a4paper, portrait, margin=1in}
\newcommand{\euler}{\begin{gather*}
\text{By Euler's formula:} \\
e^{i\theta} = cos(\theta) + i\sin(\theta) \\

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 8.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

View file

@ -1,16 +1,13 @@
{
"name": "the-honk",
"scripts": {
"prepare": "husky install",
"maths": "node .husky/scripts/cleanMaths.js"
"prepare": "husky install"
},
"devDependencies": {
"@types/node": "^17.0.6",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"husky": "^7.0.4",
"jimp": "^0.16.1",
"pdf-to-png-converter": "^1.0.0",
"prettier": "^2.5.1",
"pretty-quick": "^3.1.3"
}

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB