<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Mathematics - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Mathematics - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 24 May 2026 11:06:59 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/mathematics/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ The Math Behind Artificial Intelligence: A Guide to AI Foundations [Full Book] ]]>
                </title>
                <description>
                    <![CDATA[ "To understand is to perceive patterns." - Isaiah Berlin This is not a math book filled with complex formulas, theorems, and concepts that are hard to grasp. Instead, it’s a detailed guide where we’l ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-math-behind-artificial-intelligence-book/</link>
                <guid isPermaLink="false">695d974f512957bf332d653a</guid>
                
                    <category>
                        <![CDATA[ AI ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Artificial Intelligence ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ book ]]>
                    </category>
                
                    <category>
                        <![CDATA[ MathJax ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Tiago Capelo Monteiro ]]>
                </dc:creator>
                <pubDate>Tue, 06 Jan 2026 23:14:23 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1767723634484/4748bd8a-26a1-4d9c-89c3-1a6d07bde69e.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <blockquote>
<p>"To understand is to perceive patterns." - Isaiah Berlin</p>
</blockquote>
<p>This is <strong>not</strong> a math book filled with complex formulas, theorems, and concepts that are hard to grasp.</p>
<p>Instead, it’s a detailed guide where we’ll break complex ideas down into simpler terms.</p>
<p>Even if you only have a general understanding of algebra, you should be able to easily follow along.</p>
<h3 id="heading-heres-what-well-cover">Here’s what we’ll cover:</h3>
<ol>
<li><p><a href="#heading-chapter-1-background-on-this-book">Chapter 1: Background on this Book</a></p>
<ul>
<li><p><a href="#heading-the-objective-here">The Objective Here</a></p>
</li>
<li><p><a href="#heading-why-is-this-book-about-ai-different">Why is This Book About AI Different?</a></p>
</li>
<li><p><a href="#heading-let-me-introduce-myself">Let Me Introduce Myself</a></p>
</li>
<li><p><a href="#heading-prerequisites">Prerequisites</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-chapter-2-the-architecture-of-mathematics">Chapter 2: The Architecture of Mathematics</a></p>
<ul>
<li><p><a href="#heading-the-tree-of-mathematics-how-everything-connects">The Tree of Mathematics: How Everything Connects</a></p>
</li>
<li><p><a href="#heading-a-quick-history-of-mathematics-from-counting-to-infinity">A Quick History of Mathematics: From Counting to Infinity</a></p>
</li>
<li><p><a href="#heading-foundations-of-relativity-how-einstein-used-math-to-understand-space-and-time">Foundations of Relativity: How Einstein Used Math to Understand Space and Time</a></p>
</li>
<li><p><a href="#heading-godels-biggest-paradox-can-math-explain-itself">Gödel’s Biggest Paradox: Can Math Explain Itself?</a></p>
</li>
<li><p><a href="#heading-what-about-applied-math-and-engineering">What About Applied Math and Engineering?</a></p>
</li>
<li><p><a href="#heading-code-examples-analytical-and-numerical-approaches">Code Examples: Analytical and Numerical Approaches</a></p>
</li>
<li><p><a href="#heading-the-impact-of-a-grand-unified-theory-of-mathematics">The Impact of a Grand Unified Theory of Mathematics</a></p>
</li>
<li><p><a href="#heading-a-final-lesson-from-history">A Final Lesson From History</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-chapter-3-the-field-of-artificial-intelligence">Chapter 3: The Field of Artificial Intelligence</a></p>
<ul>
<li><p><a href="#heading-what-is-artificial-intelligence">What is Artificial Intelligence?</a></p>
</li>
<li><p><a href="#heading-symbolic-vs-non-symbolic-ai-whats-the-difference">Symbolic vs. Non-symbolic AI: What’s the Difference?</a></p>
</li>
<li><p><a href="#heading-before-ai-control-theory-as-the-first-ai">Before AI: Control Theory as the “First AI”</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-chapter-4-linear-algebra-the-geometry-of-data">Chapter 4: Linear Algebra - The Geometry of Data</a></p>
<ul>
<li><p><a href="#heading-what-are-matrices-and-why-do-they-simplify-equations">What Are Matrices and Why Do They Simplify Equations?</a></p>
</li>
<li><p><a href="#heading-vectors-and-transformations-moving-in-multiple-directions">Vectors and Transformations: Moving in Multiple Directions</a></p>
</li>
<li><p><a href="#heading-linear-independence-dependence-and-rank-why-it-matters">Linear Independence, Dependence, and Rank: Why It Matters</a></p>
</li>
<li><p><a href="#heading-determinants-measuring-space-and-scaling">Determinants: Measuring Space and Scaling</a></p>
</li>
<li><p><a href="#heading-what-are-mathematical-spaces-and-how-do-they-simplify-calculations">What Are Mathematical Spaces and How Do They Simplify Calculations?</a></p>
</li>
<li><p><a href="#heading-eigenvalues-and-eigenvectors-unlocking-hidden-patterns">Eigenvalues and Eigenvectors: Unlocking Hidden Patterns</a></p>
</li>
<li><p><a href="#heading-applications-of-linear-algebra-in-ai-and-control-theory">Applications of Linear Algebra in AI and Control Theory</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-chapter-5-multivariable-calculus-change-in-many-directions">Chapter 5: Multivariable Calculus - Change in Many Directions</a></p>
<ul>
<li><p><a href="#heading-limits-and-continuity-understanding-smooth-change">Limits and Continuity: Understanding Smooth Change</a></p>
</li>
<li><p><a href="#heading-why-are-limits-important-to-understand-derivatives-and-integrals">Why are limits important to understand derivatives and integrals?</a></p>
</li>
<li><p><a href="#heading-derivatives-how-things-change-and-how-fast">Derivatives: How Things Change and How Fast</a></p>
</li>
<li><p><a href="#heading-what-about-integral-calculus">What About Integral Calculus?</a></p>
</li>
<li><p><a href="#heading-applications-in-ai-and-control-theory-calculus-in-action">Applications in AI and Control Theory: Calculus in Action</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-chapter-6-probability-amp-statistics-learning-from-uncertainty">Chapter 6: Probability &amp; Statistics - Learning from Uncertainty</a></p>
<ul>
<li><p><a href="#heading-mean-median-mode-measuring-central-tendency">Mean, Median, Mode: Measuring Central Tendency</a></p>
</li>
<li><p><a href="#heading-variance-and-standard-deviation-measuring-spread">Variance and Standard Deviation: Measuring Spread</a></p>
</li>
<li><p><a href="#heading-what-is-the-normal-distribution-the-bell-curve-of-life">What Is the Normal Distribution? The Bell Curve of Life</a></p>
</li>
<li><p><a href="#heading-how-the-central-limit-theorem-helps-approximate-the-world">How the Central Limit Theorem Helps Approximate the World</a></p>
</li>
<li><p><a href="#heading-bayes-theorem-learning-from-evidence">Bayes Theorem: Learning from Evidence</a></p>
</li>
<li><p><a href="#heading-what-are-markov-models-predicting-the-next-step-one-step-at-a-time">What Are Markov Models? Predicting the Next Step, One Step at a Time</a></p>
</li>
<li><p><a href="#heading-applications-in-ai-and-control-theory-making-decisions-under-uncertainty">Applications in AI and Control Theory: Making Decisions Under Uncertainty</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-chapter-7-optimization-theory-teaching-machines-to-improve">Chapter 7: Optimization Theory - Teaching Machines to Improve</a></p>
<ul>
<li><p><a href="#heading-what-is-optimization-theory">What is Optimization Theory?</a></p>
</li>
<li><p><a href="#heading-why-optimization-drives-learning-in-ai">Why Optimization Drives Learning in AI</a></p>
</li>
<li><p><a href="#heading-simple-optimization-techniques-how-machines-learn-step-by-step">Simple Optimization Techniques: How Machines Learn Step by Step</a></p>
</li>
<li><p><a href="#heading-what-is-adam-the-most-popular-way-ai-models-finds-the-best-learning-path">What is Adam? The Most Popular Way AI Models Finds the Best Learning Path</a></p>
</li>
<li><p><a href="#heading-applications-in-ai-and-control-theory-of-optimization-theory">Applications in AI and Control Theory of Optimization Theory</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-conclusion-where-mathematics-and-ai-meet">Conclusion: Where Mathematics and AI Meet</a></p>
<ul>
<li><p><a href="#heading-mathematics-is-the-foundation-of-ai">Mathematics is the Foundation of AI</a></p>
</li>
<li><p><a href="#heading-the-future-on-device-ai-and-the-democratization-of-ai">The Future: On Device AI and the Democratization of AI</a></p>
</li>
<li><p><a href="#heading-final-reflections">Final Reflections</a></p>
</li>
<li><p><a href="#heading-acknowledgements">Acknowledgements</a></p>
</li>
</ul>
</li>
<li><p><a href="#heading-about-the-author">About the Author</a></p>
</li>
</ol>
<h2 id="heading-chapter-1-background-on-this-book">Chapter 1: Background on this Book</h2>
<h3 id="heading-the-objective-here">The Objective Here</h3>
<p>My objective in this book is simple: Explain the key mathematical ideas you need to grasp in order to deeply understand AI and train machine learning models.</p>
<p>So you might be wondering: Why is it important to have a good math foundation before creating these models?</p>
<p>Well, there are many reasons, but some are:</p>
<ul>
<li><p>It gives you the capacity to understand new AI research on your own.</p>
</li>
<li><p>You can use this same foundation to study other STEM concepts like signal theory and advanced statistical methods.</p>
</li>
<li><p>It helps you understand that AI models are just a mixture of different math ideas working together and gives you insight into how new innovations make LLMs more efficient.</p>
</li>
<li><p>It gives you a foundation so you know how to calibrate AI models and even create derivative models.</p>
</li>
</ul>
<p>These skills are also important for startup founders, especially in Silicon Valley. Many startups begin with APIs or API wrappers but eventually need their own AI solutions.</p>
<p>Outsourcing all AI isn't ideal. This book will help you understand AI foundations so you can design better growth strategies and communicate effectively with investors – especially those who were successful technical co-founders.</p>
<h3 id="heading-why-is-this-book-about-ai-different">Why is This Book About AI Different?</h3>
<p>In this book, we’ll look at AI from an engineering perspective. This differs from the typical computer science approach to AI that most introductory courses take.</p>
<p>In doing so, I won’t spend a lot of time explaining formulas and theorems. Instead, I’ll explain their importance, how and why they are applied the way they are.</p>
<p>In this way, I hope to offer a unique viewpoint that emphasizes the engineering principles and good practices that underlie all modern AI technologies.</p>
<p>I will also explain how many of these strange math ideas make billion dollar industries possible.</p>
<p>We’ll start with the fundamentals: the structure of the areas of mathematics and AI. After that, we’ll look at the four subareas of math that make AI possible:</p>
<ul>
<li><p>Linear Algebra</p>
</li>
<li><p>Calculus</p>
</li>
<li><p>Probability Theory and Statistics</p>
</li>
<li><p>Optimization Theory</p>
</li>
</ul>
<p>After going through all the math, we’ll connect it with the foundation of ChatGPT and all of these large language models.</p>
<p>This way, you’ll get a basic foundation in key math concepts that, when mixed together like the ingredients of a cake, make all AI models possible.</p>
<p>By knowing where the ideas come from, you’ll develop a system-level understanding of AI and a first-principles approach.</p>
<p>So just keep in mind that, even though concepts like integral calculus and eigenvalues/eigenvectors might not be widely used in AI, they’ll help you develop these system-level and first-principle approaches.</p>
<p>Also, this book will be a work in progress. After its first release, I’ll seek feedback on things I need to perfect, chapters to add, and so on.</p>
<p>Here is my email for any feedback you might have: <a href="mailto:monteiro.t@northeastern.edu">monteiro.t@northeastern.edu</a></p>
<p>And here is the book’s GitHub repository with all code: <a href="https://github.com/tiagomonteiro0715/The-Math-Behind-Artificial-Intelligence-A-Guide-to-AI-Foundations">https://github.com/tiagomonteiro0715/The-Math-Behind-Artificial-Intelligence-A-Guide-to-AI-Foundations</a></p>
<h3 id="heading-let-me-introduce-myself">Let Me Introduce Myself</h3>
<p>My name is Tiago Monteiro, an electrical and computer engineer and AI master's degree student at Northeastern University's Silicon Valley campus. I have authored 20+ articles with 240K+ views here on freeCodeCamp on math, AI, and tech.</p>
<p>If you’d like to know more about my background, I’ll share that at the end of the book.</p>
<h3 id="heading-prerequisites">Prerequisites</h3>
<p>In terms of minimum requirements, you only need to know the basics of mathematics and programming:</p>
<ul>
<li><p>Basic algebra and what functions and the coordinate system are.</p>
</li>
<li><p>You should be able to read Python code and understand things like variables, functions, and loops.</p>
</li>
</ul>
<h2 id="heading-chapter-2-the-architecture-of-mathematics">Chapter 2: The Architecture of Mathematics</h2>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766099739986/049ff3c0-0150-495e-97e9-4f16f3861058.png" alt="Cover of the chapter the architecture of mathematics" style="display:block;margin:0 auto" width="1920" height="1080" loading="lazy">

<p>Math is more than numbers. It’s the science of locating complex patterns that shape our world. To truly understand math, we must look beyond numbers and formulas to grasp its structures.</p>
<p>This chapter aims to show math as a growing tree of ideas, a living system of logic, not just formulas to memorize. With analogies, history, and code examples, I want to help you understand math deeply and how to apply it to programming.</p>
<p>I’ve included code examples to connect theory and practice, showing how math ideas apply to real problems. Whether you're new to advanced math or are more experienced, these examples will help you apply math in programming.</p>
<p>This way, before we start going over the different math pillars that sustain AI, you will understand the structure of the field.</p>
<h3 id="heading-the-tree-of-mathematics-how-everything-connects">The Tree of Mathematics: How Everything Connects</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765001557970/7ac6c8c8-d0fd-4a67-be6a-6d8b9a1a6615.jpeg" alt="Seeing a tree from its root to a tree" style="display:block;margin:0 auto" width="2000" height="1299" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/bottom-view-of-green-leaved-tree-during-daytime-91153/">Lerkrat Tangsri</a></p>
<p>Imagine math as a vast, ever-growing tree.</p>
<p>The roots are the foundations: logic and set theory. From these roots, the main fields emerge: arithmetic, algebra, geometry, and analysis.</p>
<p>As the tree branches out, new subfields like topology and abstract algebra appear. Sometimes branches connect with each other.</p>
<p>This tree keeps growing in many directions. History shows that sometimes it grows rapidly due to scientific discoveries, while at other times, growth is slow.</p>
<p>And you might wonder: How many more branches and connections between them will keep appearing?</p>
<h3 id="heading-a-quick-history-of-mathematics-from-counting-to-infinity">A Quick History of Mathematics: From Counting to Infinity</h3>
<p>The first mathematical ideas emerged independently in ancient civilizations, such as:</p>
<ul>
<li><p>India's invention of zero</p>
</li>
<li><p>Islamic algebraic advances</p>
</li>
<li><p>Greek geometric rigor</p>
</li>
</ul>
<p>Great mathematicians developed and shared these ideas through writing and lectures. Over time, new generations built on these ideas, creating new branches of mathematics. This endless growth is why Isaac Newton wrote to Robert Hooke in 1675:</p>
<blockquote>
<p>“If I have seen further, it is by standing on the shoulders of giants.”</p>
</blockquote>
<p>He meant that by working from previous knowledge, he was able to create and (re)discover new ideas.</p>
<p>Yet, the real power of math lies in practicing it over and over and studying it more and more deeply.</p>
<p>As one of my professors once pointed out:</p>
<blockquote>
<p><em>“More important than knowing the theorems is knowing the ideas behind them and the history of how they were created.”</em></p>
</blockquote>
<p>To solve problems, it's often necessary to think from first principles, and math teaches this. Math is not just an academic topic. It’s a global language for scientists and engineers.</p>
<p>By preserving and sharing it, new math can grow from old ideas, allowing the tree to keep expanding.</p>
<h3 id="heading-foundations-of-relativity-how-einstein-used-math-to-understand-space-and-time">Foundations of Relativity: How Einstein Used Math to Understand Space and Time</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766903578928/a4102586-cb63-4410-8793-72950145726d.jpeg" alt="A satellite in space" style="display:block;margin:0 auto" width="2274" height="1506" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/gray-and-white-satellite-41006/">Pixabay</a></p>
<p>Albert Einstein developed the general and special theories of relativity, which impact:</p>
<ul>
<li><p>GPS and global communication</p>
</li>
<li><p>Satellite telecommunications</p>
</li>
<li><p>Space exploration and satellite launches</p>
</li>
</ul>
<p>And more.</p>
<p>But this was only possible by combining geometry with calculus, known as <strong>differential geometry.</strong> This field evolved over centuries, thanks to many great mathematicians. Here are a few of them, though the list is not exhaustive:</p>
<ul>
<li><p><strong>Euclid (circa 300 BCE):</strong> Contributed to geometry, laying the groundwork for later mathematical systems</p>
</li>
<li><p><strong>Archimedes (circa 287–212 BCE):</strong> Pioneered the understanding of volume, surface area, and the principles of mechanics</p>
</li>
<li><p><strong>René Descartes (1596–1650):</strong> Developed Cartesian coordinates and analytical geometry</p>
</li>
<li><p><strong>Isaac Newton (1642–1727) &amp; Gottfried Wilhelm Leibniz (1646–1716):</strong> Newton’s laws of motion and gravitation, alongside Leibniz’s development of calculus, formed the basis of classical mechanics that Einstein sought to extend and modify in his theory of relativity.</p>
</li>
<li><p><strong>Leonhard Euler (1707–1783):</strong> Contributed to the development of differential equations, which are essential in the mathematical foundations of physics.</p>
</li>
<li><p><strong>Gaspard Monge (1746–1818):</strong> The father of differential geometry and pioneer in descriptive geometry</p>
</li>
<li><p><strong>Carl Friedrich Gauss (1777–1855):</strong> Made groundbreaking advances in geometry, including the concept of curved surfaces.</p>
</li>
<li><p><strong>Bernhard Riemann (1826–1866):</strong> Introduced Riemannian geometry, a branch of differential geometry.</p>
</li>
</ul>
<p>Going back to Albert Einstein, he saw what no one else in his time saw, thanks to these great math giants and countless others.</p>
<h3 id="heading-godels-biggest-paradox-can-math-explain-itself">Gödel’s Biggest Paradox: Can Math Explain Itself?</h3>
<p>The biggest paradox in math, discovered by Kurt Gödel, is his incompleteness theorems. They show that in any consistent formal system capable of simple arithmetic, there are true statements that cannot be proven within the system.</p>
<p>This means there are limits to what can be proven as true or false. For mathematicians, this implies that some truths are beyond formal proofs, yet we assume they are true. It demonstrates that no matter how much effort or AI is used, some things remain unprovable, known only through approximations and non-exact methods.</p>
<h3 id="heading-what-about-applied-math-and-engineering">What About Applied Math and Engineering?</h3>
<p>Applied math and engineering involve adapting the pure math ideas in real-world scenarios.</p>
<p>Actually, in many cases, it’s the combination of many math ideas.</p>
<p>Let’s consider some examples:</p>
<ul>
<li><p>In <strong>harmonic analysis</strong>, Laplace, Fourier, and Z-transforms are a way to see the same thing in a new domain to get new insights. In this case, integrals are used to make this mapping possible.</p>
</li>
<li><p><strong>Principal component analysis (PCA)</strong> is a widely used tool in data science. Yet, it is a mixture of linear algebra (in PCA, eigenvalues) with optimization (order eigenvalues that represent more data with less data) in order to make datasets shorter.</p>
</li>
<li><p>In <strong>machine learning</strong>, logistic regression is a mixture of calculus with statistics and probability.</p>
</li>
<li><p>In <strong>deep learning</strong>, neural networks are just many matrices multiplying and updating themselves that adapt to model a dataset representing a system. This optimization of matrix values happens with activation functions, a gradient descent-based optimization method (tells how much values need to change), and backpropagation (applies those alterations to all matrix values).</p>
</li>
</ul>
<p>But the best example of this fusion of math in engineering is in <a href="https://www.freecodecamp.org/news/basic-control-theory-with-python/">control theory</a>. Control theory is the study of the architecture of systems. From trains to cars to airplanes, everything is based on control theory. It’s everywhere, in nearly all modern electronic devices. In electric circuits, control theory is also used heavily to guarantee circuit stability in the face of electric disturbances.</p>
<p>So as you can probably start to see, many of the tools we now have are just a mixture of many pure math ideas – like different recipes. In essence, applied math is the application of pure math as “ingredients“ in "recipes" to solve problems.</p>
<p>So, we’ve explored the structure and evolution of mathematics. But it’s important to see how we can apply these ideas in real life. Pure math makes the framework, and applied math applies that framework to solve problems. To understand this, we’ll examine two code examples that show how you can use math ideas as programming tools.</p>
<h3 id="heading-code-examples-analytical-and-numerical-approaches">Code Examples: Analytical and Numerical Approaches</h3>
<p>These code examples demonstrate a couple ways you can use Python to solve math equations.</p>
<p>In the first code example, we’ll solve the problem in the same way that kids in school solve math exercises: essentially, by hand with a pencil. In the second example, we’ll solve the problem using numerical analysis.</p>
<h4 id="heading-example-1-solve-a-problem-analytically">Example 1: Solve a Problem Analytically</h4>
<p>In this problem, we need to find the values of the variables x and y. So we’ll be moving variables from left to right to find their values.</p>
<p>When we solve math problems analytically, like we did in school, we are manipulating symbols to get exact values. Often these symbols are x, y, and z.</p>
<p>The code below solves a system of two equations with two unknowns variables, x and y.</p>
<p>We will use the <a href="https://www.sympy.org">SymPy</a> Python library to do this. It’s mainly used for symbolic mathematics.</p>
<pre><code class="language-python">from sympy import symbols, Eq, solve

x, y = symbols('x y')
eq1 = Eq(2*x + 3*y, 6)
eq2 = Eq(-x + y, 1)

solution = solve((eq1, eq2), (x, y))
print(solution)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747160359386/7a21cddc-f4ba-4f9f-afa0-d1cc11fb27d6.png" alt="Image of the equations and analytical method in Python" style="display:block;margin:0 auto" width="2080" height="1224" loading="lazy">

<p>Once again with this code we are finding the values of the variables x and y.</p>
<p>Essentially, we’re finding x and y based on this equation:</p>
<p>$$\begin{align} 2x + 3y &amp;= 6 \ -x + y &amp;= 1 \end{align}$$</p>
<p>Which gives us the following result:</p>
<pre><code class="language-python">{x: 3/5, y: 8/5}
</code></pre>
<p>Or:</p>
<ul>
<li><p>x= 0.6</p>
</li>
<li><p>y = 1.6</p>
</li>
</ul>
<p>When we say that we’re solving this analytically, it means that we’re finding an exact mathematical solution using formulas or equations.</p>
<p>But many times, problems are harder and can be solved by adding symbols to the right or left of the equation. Sometimes, there can be so many symbols and transformed versions of them, with things like derivatives and integrals, that it can become very hard to manage and takes a lot of time.</p>
<p>For example, let’s look at this partial differential equation:</p>
<p>$$\begin{cases} \frac{\partial u}{\partial t} = \alpha \frac{\partial^2 u}{\partial x^2}, &amp; 0 &lt; x &lt; L, , t &gt; 0 \ u(0,t) = 0, &amp; t &gt; 0 \ u(L,t) = 0, &amp; t &gt; 0 \ u(x,0) = f(x), &amp; 0 &lt; x &lt; L \end{cases}$$</p>
<p>It can be solved with an analytical method call separation of variables.</p>
<p>But it requires many steps, and it’s easy to make mistakes. Even engineers who learned this often struggle to remember the process later.</p>
<p>When I first encountered this type of math exercise in my electrical and computer engineering degree back in Portugal, it took me 20 to 30 minutes to solve it.</p>
<p>For this reason, there's a branch of mathematics called numerical analysis that focuses on finding approximations of existing formulas. It helps solve problems faster. This is the method we'll explore next.</p>
<h4 id="heading-example-2-solve-numerically-approximation">Example 2: Solve Numerically (Approximation)</h4>
<p>Now let’s solve a different problem: we’re going to find the values of each of the 5 variables:</p>
<p>$$\begin{bmatrix} 3 &amp; 2 &amp; -1 &amp; 4 &amp; 5 \ 1 &amp; 1 &amp; 3 &amp; 2 &amp; -2 \ 4 &amp; -1 &amp; 2 &amp; 1 &amp; 0 \ 5 &amp; 3 &amp; -2 &amp; 1 &amp; 1 \ 2 &amp; -3 &amp; 1 &amp; 3 &amp; 4 \end{bmatrix} \times \begin{bmatrix} x_1 \ x_2 \ x_3 \ x_4 \ x_5 \end{bmatrix} = \begin{bmatrix} 12 \ 5 \ 7 \ 9 \ 10 \end{bmatrix}$$</p>
<p>Solving this by hand will take some time…but with Python code, it’s very fast.</p>
<p>We’ll also use the <a href="https://scipy.org">SciPy</a> Python library for this example.</p>
<p>Let’s solve the system numerically:</p>
<pre><code class="language-python">import numpy as np
from scipy.linalg import solve

A = np.array([[3, 2, -1, 4, 5],
              [1, 1, 3, 2, -2],
              [4, -1, 2, 1, 0],
              [5, 3, -2, 1, 1],
              [2, -3, 1, 3, 4]])

b = np.array([12, 5, 7, 9, 10])

solution = solve(A, b)

print(solution)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747160347486/d1f17aa6-b288-4e41-9be7-0810c45e778c.png" alt="Image of equations and numerical method" style="display:block;margin:0 auto" width="2080" height="1764" loading="lazy">

<p>Which corresponds to this operation:</p>
<p>$$\begin{bmatrix} 3 &amp; 2 &amp; -1 &amp; 4 &amp; 5 \ 1 &amp; 1 &amp; 3 &amp; 2 &amp; -2 \ 4 &amp; -1 &amp; 2 &amp; 1 &amp; 0 \ 5 &amp; 3 &amp; -2 &amp; 1 &amp; 1 \ 2 &amp; -3 &amp; 1 &amp; 3 &amp; 4 \end{bmatrix} \times \begin{bmatrix} x_1 \ x_2 \ x_3 \ x_4 \ x_5 \end{bmatrix} = \begin{bmatrix} 12 \ 5 \ 7 \ 9 \ 10 \end{bmatrix}$$</p>
<p>Again, it takes time to solve this and it’s very easy to make a simple mistake.</p>
<p>But in this code example, this line of code:</p>
<pre><code class="language-python">solution = solve(A, b)
</code></pre>
<p>Uses the <code>solve</code> method from SciPy:</p>
<pre><code class="language-python">from scipy.linalg import solve
</code></pre>
<p>It’s a method that helps you find the values of x in an equation A⋅x=b, where A is a square grid of numbers and b is a list of numbers. That gives us the following:</p>
<pre><code class="language-python">[ 1.35022026 -0.79955947 -1.17180617  3.14317181 -0.83920705]
</code></pre>
<p>Which corresponds to:</p>
<p>$$\begin{bmatrix} x_1 \ x_2 \ x_3 \ x_4 \ x_5 \end{bmatrix} = \begin{bmatrix} 1.35022026 \ -0.79955947 \ -1.17180617 \ 3.14317181 \ -0.83920705 \end{bmatrix}$$</p>
<p>And is the same thing as:</p>
<p>$$\begin{align} x_1 &amp;= 1.35022026 \ x_2 &amp;= -0.79955947 \ x_3 &amp;= -1.17180617 \ x_4 &amp;= 3.14317181 \ x_5 &amp;= -0.83920705 \end{align}$$</p>
<h4 id="heading-why-these-two-approaches-matter">Why These Two Approaches Matter</h4>
<p>We have solved two mathematical problems in two different ways:</p>
<ul>
<li><p>Analytical: Exact solutions through algebraic manipulation</p>
</li>
<li><p>Numerical: Approximate solutions using algorithms</p>
</li>
</ul>
<p>In engineering and in AI, we are constantly choosing between these approaches.</p>
<p>When training AI models with millions of parameters, analytical solutions are impossible. This is why, in these cases, we need numerical approaches.</p>
<p>When creating math theorems, we need analytical precision to make sure it is the best possible solution.</p>
<p>This is one of the many things an engineering degree teaches you: often, in the real world, it’s better to just write some code to solve a problem than to actually solve it by hand with math. Other times, the best solution is to just think in first principles and from there create new theorems to solve a problem.</p>
<p>Now let's step out of the code examples and see how different branches of mathematics connect.</p>
<h3 id="heading-the-impact-of-a-grand-unified-theory-of-mathematics">The Impact of a Grand Unified Theory of Mathematics</h3>
<p>Is it possible to unify all math?</p>
<p>In theory, yes. This is known as the Grand Unified Theory of Mathematics. It's the idea that all different areas of math can be linked together to discover deeper patterns in mathematics.</p>
<p>The <a href="https://en.wikipedia.org/wiki/Langlands_program">Langlands program</a> is trying to make this unification possible. It’s an attempt to interconnect the largest parts of the big tree of math to uncover new patterns in math.</p>
<p>With a Grand Unified Theory of Mathematics, we would be able to understand how every branch of the tree connects with the others and all the relationships between them.</p>
<h4 id="heading-whats-the-value-of-this-big-unification-for-society">What’s the Value of this Big Unification for Society?</h4>
<p>By studying history, we can find patterns. The unification of various fields has created many massive impacts on society, such as:</p>
<ul>
<li><p>In the 19th century, James Clerk Maxwell united the fields of electricity and magnetism with his famous Maxwell equations. This allowed the creation of radios and electric grids around the globe. In turn, it served as a foundation for all technological progress in the 20th and 21st century.</p>
</li>
<li><p>In the 20th century, the unification of algebra with logic led to the rise of digital systems. In turn, digital systems gave rise to processors and the evolution of computers and the modern laptop.</p>
</li>
<li><p>Also in the 20th century, the unification of probability and communication led to information theory. This became the foundation for the internet. This unification was carried out by a great mathematician named Claude Shannon.</p>
</li>
</ul>
<p>In the end, a grand unified theory of mathematics could be one of the biggest achievements in modern society.</p>
<p>In AI, it could help unify all machine learning models in a common architecture. This would help accelerate the development of new AI models and could also open the door to new material science advances.</p>
<p>It could help reveal – with math – the deep patterns we still haven’t found in these fields. Just as uniting electricity and magnetism led to modern technology, a unified math framework would lead to a wave of innovation.</p>
<h3 id="heading-a-final-lesson-from-history">A Final Lesson From History</h3>
<p>From Greek geometry to AI, math has grown like a tree over centuries. By understanding its structure, it’s possible to see its role in finding the patterns of our universe.</p>
<p>I hope I was able to make you see math in this way. I hope you can also see that the unification of scientific fields helps lay the foundations for the creation of new innovations to help society go forward.</p>
<p>Many major societal transformations only came to be thanks to abstract math ideas. When these are shared and refined, they become the hidden architecture of progress in society. Innovation begins when disconnected ideas are united, well-linked, and widely shared.</p>
<h2 id="heading-chapter-3-the-field-of-artificial-intelligence">Chapter 3: The Field of Artificial Intelligence</h2>
<h3 id="heading-what-is-artificial-intelligence">What is Artificial Intelligence?</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765001693682/bbec3565-643f-421f-b32e-3de62285a2c0.jpeg" alt="A man playing chess against a robot" style="display:block;margin:0 auto" width="5192" height="3466" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/elderly-man-thinking-while-looking-at-a-chessboard-8438918/">Pavel Danilyuk</a></p>
<p>The term Artificial Intelligence was born from the work of John McCarthy, who is often called the "father of AI."</p>
<p>He used it when he, along with Marvin Minsky, Nathaniel Rochester, and Claude Shannon, proposed the famous Dartmouth Summer Research Project on Artificial Intelligence in 1956.</p>
<p>Artificial intelligence was defined, in the Dartmouth Conference, as:</p>
<blockquote>
<p><em>“Every aspect of learning or any other feature of intelligence can in principle be so precisely described that a machine can be made to simulate it.”</em></p>
</blockquote>
<p>Since then, the field has evolved in waves of innovation, from early rules-based systems to modern neural networks.</p>
<p>But over time, rather than creating <a href="https://en.wikipedia.org/wiki/Artificial_general_intelligence">general intelligence</a>, most AI systems have been designed to excel at narrow tasks.</p>
<p>For example:</p>
<ul>
<li><p>Chess-playing programs like Deep Blue that defeated world champion Garry Kasparov</p>
</li>
<li><p>Image recognition systems that can identify objects in photographs with impressive accuracy</p>
</li>
<li><p>Natural language processing models that can translate between languages</p>
</li>
<li><p>Game-playing AI like AlphaGo that mastered the ancient game of Go</p>
</li>
</ul>
<h4 id="heading-artificial-general-intelligence-isnt-yet-here">Artificial General Intelligence isn’t yet here</h4>
<p>Only very narrow AI models have demonstrated human-level or superhuman performance in their narrow domains.</p>
<p>In my view, and as we will see in this book, AGI will be the combination and interaction of different large language models interacting with each other and with the tools available to them.</p>
<h3 id="heading-symbolic-vs-non-symbolic-ai-whats-the-difference">Symbolic vs. Non-symbolic AI: What’s the Difference?</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1755906822438/f639efd3-3f8b-45a7-ad2d-d1795d772947.png" alt="Image comparing artificial general intelligence with narrow AI and, inside narrow AI, non-symbolic AI and symbolic AI circles" style="display:block;margin:0 auto" width="1858" height="1041" loading="lazy">

<h4 id="heading-what-is-symbolic-ai">What is Symbolic AI?</h4>
<p>Symbolic AI refers to the creation of a program based on many rules and symbols to simulate how humans think.</p>
<p>It uses symbols to represent concepts (like farms and distributors) and logical rules to reason about them.</p>
<p>The specific data about your domain is called facts. Facts are the pieces of information the rules operate on. For example, a fact might be "green_acres has high water usage and good pH levels."</p>
<p>Also, imagine someone wants to optimize farm distribution logistics. The symbols would represent farms, distributors, and transport methods. Then the rules would be:</p>
<ul>
<li><p>If the farm has high water usage and good pH levels, then classify it as high-yield producer</p>
</li>
<li><p>If a high-yield producer and distributor has low demand, then prioritize direct connection</p>
</li>
<li><p>If a direct connection is needed, then select transport with lowest environmental impact</p>
</li>
</ul>
<p>The facts would be the actual data like "farm X has high water usage" or "distributor Y has low demand."</p>
<p>This way, the system combines these rules and facts through logical reasoning to make decisions. A very popular programming language we use in this field is called Prolog that was designed to create rule-based systems.</p>
<p><strong>Symbolic AI program: Manage agricultural networks with a Prolog program.</strong></p>
<p>Let’s look at an example project to understand this more clearly. The project we’ll examine is called SymbolicAIHarvest. It was part of a course at NOVA University during my undergraduate studies in Electrical and Computer Engineering. The course was titled "Modelation of Data in Engineering."</p>
<p>SymbolicAIHarvest is an AI system developed with Prolog to manage agricultural networks. <a href="https://github.com/tiagomonteiro0715/SymbolicAIHarvest">Here’s the project</a> on GitHub so you can check it out.</p>
<p>The project optimizes farm operations using rule-based reasoning. It monitors sensors for real-time data and improves route planning for machinery. It also coordinates produce movement to reduce delays and waste, enhancing productivity and sustainability.</p>
<p>Understanding the code below is not a priority for this book. I just want to show you an example of all the facts of the project:</p>
<pre><code class="language-plaintext">% FARMERS(owner)
farmer(ana).
farmer(asdrubal).
farmer(miguel).
farmer(joao).
farmer(teresinha).
farmer(victor).
farmer(carlos).
farmer(anabela).

% FARMS(name, owner, region, type)
farm(q1, ana, alentejo, vinha).
farm(q2, ana, alentejo, olival).
farm(q3, asdrubal, lisboa, cenoureira).
farm(q4, asdrubal, lisboa, milharal).
farm(q5, asdrubal, lisboa, vinha).
farm(q6, miguel, evora, trigal).
farm(q7, miguel, evora, cenoureia).
farm(q8, miguel, evora, vinha).
farm(q9, miguel, evora, morangueira).
farm(q10, joao, porto, vinha).
farm(q11, joao, porto, trigal).
farm(q12, joao, porto, cenoureira).
farm(q13, teresinha, algarve, olival).
farm(q14, teresinha, algarve, vinha).
farm(q15, victor, setubal, olival).
farm(q16, victor, setubal, vinha).
farm(q17, victor, setubal, trigal).
farm(q18, carlos, sintra, milharal).
farm(q19, carlos, sintra, vinha).
farm(q20, anabela, coina, milharal).
farm(q21, anabela, coina, olival).
farm(q22, anabela, coina, trigal).

% SENSOR READINGS(name, type, value)
sensor_reading(q1,humidity,28).
sensor_reading(q2,humidity,35).
sensor_reading(q3,humidity,42).
sensor_reading(q4,humidity,38).
sensor_reading(q5,humidity,33).
sensor_reading(q6,humidity,45).
sensor_reading(q7,humidity,30).
sensor_reading(q8,humidity,36).
sensor_reading(q9,humidity,50).
sensor_reading(q10,humidity,41).
sensor_reading(q11,humidity,40).
sensor_reading(q12,humidity,44).
sensor_reading(q13,humidity,32).
sensor_reading(q14,humidity,29).
sensor_reading(q15,humidity,47).
sensor_reading(q16,humidity,39).
sensor_reading(q17,humidity,53).
sensor_reading(q18,humidity,27).
sensor_reading(q19,humidity,24).
sensor_reading(q20,humidity,31).
sensor_reading(q21,humidity,37).
sensor_reading(q22,humidity,46).
sensor_reading(q1, temperature, 25).
sensor_reading(q2, temperature, 25).
sensor_reading(q3, temperature, 25).
sensor_reading(q4, temperature, 25).
sensor_reading(q5, temperature, 25).
sensor_reading(q6, temperature, 25).
sensor_reading(q7, temperature, 25).
sensor_reading(q8, temperature, 25).
sensor_reading(q9, temperature, 25).
sensor_reading(q10, temperature, 25).
sensor_reading(q11, temperature, 25).
sensor_reading(q12, temperature, 25).
sensor_reading(q13, temperature, 25).
sensor_reading(q14, temperature, 25).
sensor_reading(q15, temperature, 25).
sensor_reading(q16, temperature, 25).
sensor_reading(q17, temperature, 25).
sensor_reading(q18, temperature, 25).
sensor_reading(q19, temperature, 25).
sensor_reading(q20, temperature, 25).
sensor_reading(q21, temperature, 25).
sensor_reading(q22, temperature, 25).
sensor_reading(q1, water, 47000).
sensor_reading(q2, water, 52500).
sensor_reading(q3, water, 39000).
sensor_reading(q5, water, 61000).
sensor_reading(q8, water, 58000).
sensor_reading(q10, water, 43000).
sensor_reading(q13, water, 72000).
sensor_reading(q16, water, 49000).
sensor_reading(q18, water, 35000).
sensor_reading(q21, water, 66500).
sensor_reading(q1, ph, 6.5).
sensor_reading(q2, ph, 4.7).
sensor_reading(q3, ph, 8.2).
sensor_reading(q4, ph, 7.0).
sensor_reading(q5, ph, 5.1).
sensor_reading(q6, ph, 8.0).
sensor_reading(q7, ph, 4.5).

% DISTRIBUTORS (name, region, capacity, demand level)
distributor(d1, alentejo, 1000, 2).
distributor(d2, lisboa, 800, 1).
distributor(d3, evora, 1200, 3).
distributor(d4, porto, 900, 2).
distributor(d5, algarve, 700, 2).
distributor(d6, setubal, 1100, 1).
distributor(d7, sintra, 950, 2).
distributor(d8, coina, 1000, 1).

% TRANSPORTS (name, capacity, type, autonomy, region, impact)
transport(t1, 1000, fossil, 100, alentejo, 3).
transport(t2, 500, electric, 10, alentejo, 1).
transport(t3, 800, fossil, 400, algarve, 5).
transport(t4, 700, hybrid, 300, setubal, 2).
transport(t5, 150, electric, 340, coina, 1).
transport(t6, 700, fossil, 220, porto, 3).
transport(t7, 900, hybrid, 350, evora, 2).
transport(t8, 1000, electric, 170, sintra, 1).

% Connections based on graph image

% Top of the network
link(q2, d1, 5).
link(q1, d1, 7).
link(q3, d1, 6).

% Network center
link(q3, q4, 8).
link(q4, d2, 6).
link(q4, d3, 7).
link(q4, q5, 5).
link(q4, d4, 6).

% Additional connections
link(q2, d2, 8).
link(q3, d3, 7).
</code></pre>
<p>This Prolog code models an agricultural supply chain system that has:</p>
<ul>
<li><p>Farmers</p>
</li>
<li><p>Farms</p>
</li>
<li><p>Sensors Readings</p>
</li>
<li><p>Distributors</p>
</li>
<li><p>Transports</p>
</li>
</ul>
<p>In addition, in this part of the code on the facts of the system:</p>
<pre><code class="language-plaintext">% Top of the network
link(q2, d1, 5).
link(q1, d1, 7).
link(q3, d1, 6).

% Network center
link(q3, q4, 8).
link(q4, d2, 6).
link(q4, d3, 7).
link(q4, q5, 5).
link(q4, d4, 6).

% Additional connections
link(q2, d2, 8).
link(q3, d3, 7).
</code></pre>
<p>We connect farms with distributors. This way, we can see that between the farm <code>q1</code> and distributor <code>d1</code> is a distance of 7k. This makes it possible to find/create algorithms to find the shortest path between them.</p>
<p>In the end, symbolic AI just creates programs based on a context and rules applied to that context.</p>
<h4 id="heading-what-is-non-symbolic-ai">What is Non-Symbolic AI?</h4>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1755906892854/197f7bc3-8c05-46f2-aa2a-99dbaa733a9a.png" alt="Non-symbolic AI with a circle titled machine learning inside. Inside the machine learning circle is another circle with the text deep learning." style="display:block;margin:0 auto" width="1711" height="951" loading="lazy">

<p>Non symbolic AI doesn’t use symbols or rules to think. Instead, it’s data driven. In other words, it learns patterns from large datasets. This is the approach used in machine learning and deep learning.</p>
<p>When we create an AI model, we can associate it with an API (Application Programming Interface) so that we can use the AI model in websites, applications, and other systems. Basically, the trained AI model is set up behind an API endpoint. An API endpoint is like a web service that lets other applications send requests to the model and get responses back.</p>
<p>For example, when you use ChatGPT in a web browser, your messages are sent through OpenAI's API to their language model, which processes your input and sends back a response.</p>
<p>An AI agent is a software program that can autonomously perform tasks by making decisions and taking actions to achieve specific goals.</p>
<p>Unlike basic chatbots that only reply to questions, AI agents can plan steps, use tools, and work towards achieving complex goals. They do this by combining language models with extra features like accessing outside data or working with other AI agents.</p>
<p><a href="https://github.com/tiagomonteiro0715/ai-content-lab">Here’s an example</a> of a non-symbolic AI agent project I worked on. I developed it using the <a href="https://www.crewai.com/">crewAI</a> Python library and the OpenAI API, one of the most popular libraries for creating AI agents.</p>
<p>In this system, five AI agents collaborate to create optimized content:</p>
<ul>
<li><p><strong>Research and Fact Checker:</strong> Conducts research to find trends and data.</p>
</li>
<li><p><strong>Audience Specialist:</strong> Analyzes audience needs for better engagement.</p>
</li>
<li><p><strong>Lead Content Writer:</strong> Writes engaging content based on research.</p>
</li>
<li><p><strong>Senior Editorial Director:</strong> Ensures content quality and consistency.</p>
</li>
<li><p><strong>SEO Specialist:</strong> Optimizes content for search engines.</p>
</li>
</ul>
<p>Using the OpenAI API, it employs chatGPT with crewAI to have these agents work for me.</p>
<h3 id="heading-before-ai-control-theory-as-the-first-ai">Before AI: Control Theory as the “First AI”</h3>
<p>Before symbolic and non symbolic AI, electrical engineering had data-driven methods. One key area that I’ve already mentioned above was control theory (which studies control systems for machines like cars and rockets). This field allows us to design systems that ensure stability despite disturbances and achieve goals beyond human capabilities.</p>
<p>Nowadays, after creating a control theory algorithm, we check if AI can improve the control system. In my experience, only some advanced deep learning methods are effective. Most machine learning methods don't outperform control theory in efficiency and security.</p>
<p>Control theory also offers better interpretability, allowing us to understand decisions, unlike advanced machine learning and deep learning.</p>
<p>Due to the historical importance of control theory, I will continue to mention its role and mathematical applications. This will help you learn AI's math foundations and understand its significance in electronic systems and AI applications in engineering beyond dataset predictions.</p>
<h2 id="heading-chapter-4-linear-algebra-the-geometry-of-data">Chapter 4: Linear Algebra - The Geometry of Data</h2>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765002362611/905a356e-7686-4212-94ac-2b4a5b359c8a.jpeg" alt="Magnifying glass pointing at a book" style="display:block;margin:0 auto" width="4272" height="2848" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/monochrome-photo-of-math-formulas-3729557/">Nothing Ahead</a>.</p>
<p>Linear algebra is like having organized containers for data.</p>
<p>Instead of playing with individual numbers, we can pack them into structured boxes that are easier to handle. These structured boxes are called matrices.</p>
<p>When you have a lot of variables like customer data, sensor readings, or images, these structured boxes are very helpful. Also, what we can do when we play around with these boxes is very valuable.</p>
<p>In AI, linear algebra is everywhere. Take matrices, for example – a key concept in Linear Algebra. LLMs perform many matrix multiplications as their core operation. The data that they take in is also organized into matrices. In image recognition, matrices are used to represent pixels of images.</p>
<p>So as you can see, this core Linear Algebra concept is important to understand. Let's start!</p>
<h3 id="heading-what-are-matrices-and-why-do-they-simplify-equations">What Are Matrices and Why Do They Simplify Equations?</h3>
<p>Very often, systems in the real world can be simplified and modeled with a system of equations.</p>
<p>Those equations are often differential equations of many orders. But to simplify, let’s choose a very simple system like the one below:</p>
<p>$$\begin{align} 2x + 3y - z &amp;= 7 \ x - 2y + 4z &amp;= -1 \ 3x + y + 2z &amp;= 10 \end{align}$$</p>
<p>When dealing with many variables and equations, writing each equation separately quickly becomes frustrating. Matrices provide a compact way to represent these systems.</p>
<p>For example, here’s the system above as a single matrix equation:</p>
<p>$$\begin{bmatrix} 2 &amp; 3 &amp; -1 \ 1 &amp; -2 &amp; 4 \ 3 &amp; 1 &amp; 2 \end{bmatrix} \begin{bmatrix} x \ y \ z \end{bmatrix} = \begin{bmatrix} 7 \ -1 \ 10 \end{bmatrix}$$</p>
<p>By seeing systems of equations as matrices, we can use linear algebra techniques to understand how the system behaves.</p>
<p>Some of these techniques are:</p>
<ul>
<li><p>Linear Independence, Dependence, and Rank</p>
</li>
<li><p>Determinants</p>
</li>
<li><p>Eigenvalues and Eigenvectors</p>
</li>
</ul>
<p>So to summarize:</p>
<ol>
<li><p>A real world system can be represented as a system of equations</p>
</li>
<li><p>A system of equations can be compressed in a structured manipulable form called a matrix.</p>
</li>
<li><p>With matrices and linear algebra techniques, we can understand how the system works.</p>
</li>
</ol>
<p>This way, we can study the basic behavior of a system with Linear Algebra.</p>
<p>For complex systems like a rocket, Linear Algebra is still the foundation. More advanced tools from control theory are used, but understanding simpler systems is essential for modeling and creating complex ones.</p>
<h3 id="heading-vectors-and-transformations-moving-in-multiple-directions">Vectors and Transformations: Moving in Multiple Directions</h3>
<p>Vectors are matrices <strong>with a single row or a single column.</strong> You can also think of them as the building blocks of AI. They represent things like data points, model parameters, and much more.</p>
<p>For example, every data input (like an image or sentence) becomes a vector that the model can processes.</p>
<p>Here are two examples of vectors:</p>
<p>$$\mathbf{A} = \begin{bmatrix} 4 &amp; -2 &amp; 7 &amp; 1 &amp; 5 \end{bmatrix}$$</p>
<p>And:</p>
<p>$$\mathbf{B} = \begin{bmatrix} 3 \ -1 \ 8 \ 0 \ -4 \end{bmatrix}$$</p>
<p>All operations that you can perform on matrices can also be performed on vectors.</p>
<p>In Python, we can represent this by:</p>
<pre><code class="language-plaintext">import numpy as np

# Define vectors A and B
A = np.array([4, -2, 7, 1, 5])
B = np.array([3, -1, 8, 0, -4])
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756171163870/4fa7dc5d-5b68-4baf-a211-3db0c3915781.png" alt="Python code image representing the code above. Defining two NumPy arrays." style="display:block;margin:0 auto" width="2080" height="844" loading="lazy">

<p>We’re using the <a href="https://numpy.org/">NumPy</a> library because it makes math with arrays easy and fast.</p>
<p>As a simplification of a system of equations, a vector with a single row represents:</p>
<p>$$\mathbf{A} = \begin{bmatrix} 4 &amp; -2 &amp; 7 &amp; 1 &amp; 5 \end{bmatrix}$$</p>
<p>And this represents this system of equations:</p>
<p>$$4x_1 - 2x_2 + 7x_3 + x_4 + 5x_5 = k$$</p>
<p>A vector with a single column represents:</p>
<p>$$\mathbf{B} = \begin{bmatrix} 3 \ -1 \ 8 \ 0 \ -4 \end{bmatrix}$$</p>
<p>Which represents this system of equations:</p>
<p>$$\begin{align} x_1 &amp;= 3 \ x_2 &amp;= -1 \ x_3 &amp;= 8 \ x_4 &amp;= 0 \ x_5 &amp;= -4 \end{align}$$</p>
<p>Now let’s see some matrix operations.</p>
<p>For example:</p>
<p>$$\mathbf{A} + \mathbf{B}^T = \begin{bmatrix} 4 &amp; -2 &amp; 7 &amp; 1 &amp; 5 \end{bmatrix} + \begin{bmatrix} 3 &amp; -1 &amp; 8 &amp; 0 &amp; -4 \end{bmatrix} = \begin{bmatrix} 7 &amp; -3 &amp; 15 &amp; 1 &amp; 1 \end{bmatrix}$$</p>
<pre><code class="language-plaintext">vector_addition = A + B
print("A + B =", vector_addition)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756171174149/62309c55-a5c5-4f69-aef6-e8ab341b5926.png" alt="Python code image representing the code above. Adding two NumPy arrays." style="display:block;margin:0 auto" width="2080" height="572" loading="lazy">

<p>Which gives the result of the equation above.</p>
<p>Often, vector addition is used to combine features. For example, adding many user preference vectors creates a profile of a user.</p>
<p>Here’s a <strong>scalar multiplication:</strong></p>
<p>$$3\mathbf{A} = 3\begin{bmatrix} 4 &amp; -2 &amp; 7 &amp; 1 &amp; 5 \end{bmatrix} = \begin{bmatrix} 12 &amp; -6 &amp; 21 &amp; 3 &amp; 15 \end{bmatrix}$$</p>
<pre><code class="language-plaintext">scalar_mult = 3 * A
print("3 * A =", scalar_mult)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756171180976/17e260a4-baab-4866-ba30-fc12e090b87a.png" alt="Python code image representing the code above. Multiplying a NumPy array with a scalar." style="display:block;margin:0 auto" width="2080" height="572" loading="lazy">

<p>Which gives the result of the equation above.</p>
<p>In AI, scaling vectors is usually done to adjust relevancy. For example, if we do a scalar product multiplication of a vector by 100, it means we are increasing its value. If it is by 0.3, it means we are reducing its importance.</p>
<p>Here's an outer product multiplication:</p>
<p>$$\mathbf{A} \otimes \mathbf{B} = \begin{bmatrix} 4 \ -2 \ 7 \ 1 \ 5 \end{bmatrix} \times \begin{bmatrix} 3 &amp; -1 &amp; 8 &amp; 0 &amp; -4 \end{bmatrix} = \begin{bmatrix} 12 &amp; -4 &amp; 32 &amp; 0 &amp; -20 \ -6 &amp; 2 &amp; -16 &amp; 0 &amp; 8 \ 21 &amp; -7 &amp; 56 &amp; 0 &amp; -28 \ 3 &amp; -1 &amp; 8 &amp; 0 &amp; -4 \ 15 &amp; -5 &amp; 40 &amp; 0 &amp; -20 \end{bmatrix}$$</p>
<p>And here’s a <strong>dot product multiplication</strong> (also called a <strong>dot product</strong>):</p>
<p>$$\mathbf{A} \cdot \mathbf{B}^T = \begin{bmatrix} 4 &amp; -2 &amp; 7 &amp; 1 &amp; 5 \end{bmatrix} \cdot \begin{bmatrix} 3 &amp; -1 &amp; 8 &amp; 0 &amp; -4 \end{bmatrix}$$</p>
<p>$$= 4 \cdot 3 + (-2) \cdot (-1) + 7 \cdot 8 + 1 \cdot 0 + 5 \cdot (-4) = 50$$</p>
<p>We mainly use dot products when we want to measure similarity, or alignment between two vectors.</p>
<p>In machine learning, in one simple phrase, it gives us a measure of similarity.</p>
<pre><code class="language-plaintext">import numpy as np

dot_product = np.dot(A, B)
print("A · B =", dot_product)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756171200508/ee7b9e61-c1cb-497d-b038-b6a672c6d24b.png" alt="Python code image representing the code above. Multiplying a NumPy array via dot product." style="display:block;margin:0 auto" width="2080" height="752" loading="lazy">

<p>Which gives the result of the equation above.</p>
<h3 id="heading-linear-independence-dependence-and-rank-why-it-matters">Linear Independence, Dependence, and Rank: Why It Matters</h3>
<p>A lot of times, matrices can be made smaller and simpler. So it’s a good practice to reduce a matrix to its simplest form before we start to analyze its properties.</p>
<p>When each row of a matrix can be made with other rows, then that matrix is linearly dependent. This means the matrix can be further modified.</p>
<p>This way, a matrix&nbsp; has the property of linear independence when its rows cannot be created by combining each other.</p>
<p>For example, when we have a complex matrix like this one:</p>
<p>$$C = \begin{bmatrix} 1 &amp; 2 &amp; 3 &amp; 4 \ 2 &amp; 4 &amp; 6 &amp; 8 \ 1 &amp; 3 &amp; 5 &amp; 7 \ 0 &amp; 1 &amp; 2 &amp; 3 \end{bmatrix}$$</p>
<p>We can, with calculations, convert to this:</p>
<p>$$C_{\text{reduced}} = \begin{bmatrix} 1 &amp; 0 &amp; -1 &amp; -2 \ 0 &amp; 1 &amp; 2 &amp; 3 \ 0 &amp; 0 &amp; 0 &amp; 0 \ 0 &amp; 0 &amp; 0 &amp; 0 \end{bmatrix}$$</p>
<p>if you are not familiar with row reduction, I recommend <a href="https://www.youtube.com/watch?v=eDb6iugi6Uk">this YouTube video</a>.</p>
<p>The above simplified matrix is the same thing as this:</p>
<p>$$C_{\text{reduced}} = \begin{bmatrix} 1 &amp; 0 &amp; -1 &amp; -2 \ 0 &amp; 1 &amp; 2 &amp; 3 \end{bmatrix}$$</p>
<p>This way, we conclude that the C matrix has a <strong>rank</strong> of 2.</p>
<p>In other words, since the simplest form of the matrix has only 2 rows with numbers, it has a rank of 2.</p>
<p>From this, we can conclude that the reduced version of the matrix is <strong>linearly independent</strong>. This is because no row or column can be made from the existing rows or column. It’s the simplest possible matrix.</p>
<p>The original matrix C is linearly dependent because some rows are just multiples or combinations of other rows. For example, row 2 of the original matrix C is exactly row 1 multiplied by 2.</p>
<p>Another way of seeing this is that we have 4 rows in the original matrix and the rank of matrix C is 2. Since they are not equal, C is linearly dependent.</p>
<h4 id="heading-why-are-these-concepts-important">Why are these concepts important?</h4>
<p>Linear independence and rank are important in engineering because they show whether equations, represented as matrices, give unique information. In electrical circuits and control systems, knowing that equations, represented as matrices, are independent ensures that you have unique solutions and avoids confusion.</p>
<p>The matrix rank shows the maximum number of independent equations that can exist. This help engineers model the simplest possible form of the systems.</p>
<p>In LLMs like ChatGPT, Gemini, Grok, and Claude, linear independence, dependence, and rank are used in a very important technique called LoRA (Low-Rank Adaptation).</p>
<p>LoRA (Low-Rank Adaptation) is widely used to calibrate these models to make sure they adapt efficiently to new tasks or domains without retraining the full model. Also, there are variants of this technique, like Quantized LoRA. This way, in many data centers, LoRA saves energy, water for cooling, and so many other things.</p>
<h3 id="heading-determinants-measuring-space-and-scaling">Determinants: Measuring Space and Scaling</h3>
<p>Why are determinants important?</p>
<p>Determinants tell us if a system of equations has infinite solutions, no solutions, or if it has a unique solution without having to solve the whole system.</p>
<p>This way, instead of immediately trying to solve a complex system, we can first use the determinant to find out if it is even worth solving in the first place.</p>
<p>Many engineers don’t really understand the importance of the determinant. The only thing they know is the formula and how to apply it.</p>
<p>So now let’s learn, with some examples, what exactly the determinant is and why it matters.</p>
<p>A determinant is just a number. It’s always calculated from a square matrix. By calculating the determinant, we can find certain properties about the system it represents.</p>
<p>The determinant of a given matrix A:</p>
<p>$$A = \begin{bmatrix} a &amp; b \ c &amp; d \end{bmatrix}.$$</p>
<p>can be represented by two notations:</p>
<p>$$\det(A) = ad - bc$$</p>
<p>or</p>
<p>$$|A| = ad - bc$$</p>
<p>Both are the same thing.</p>
<p>Let's see how to calculate a determinant:</p>
<p>$$|A| = \begin{vmatrix} 2 &amp; 3 \ 1 &amp; 4 \end{vmatrix} = (2)(4) - (3)(1) = 8 - 3 = 5.$$</p>
<p>Let’s see how to do this in Python:</p>
<pre><code class="language-plaintext">import numpy as np

# Define the matrix
A = np.array([
    [2, 3],
    [1, 4]
])

# Calculate the determinant
det_A = np.linalg.det(A)

print("Determinant of A:", det_A)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756233259727/feea57a3-5a33-49b9-a74a-979eba5ec7fe.png" alt="Python code image representing the code above. Finding the determinant." style="display:block;margin:0 auto" width="2080" height="1472" loading="lazy">

<h4 id="heading-the-same-calculation-works-for-other-matrices">The same calculation works for other matrices!</h4>
<p>Here's the determinant formula for a 3×3 matrix:</p>
<p>For a 3 by 3 matrix:</p>
<p>$$|B|= \begin{vmatrix} a &amp; b &amp; c \ d &amp; e &amp; f \ g &amp; h &amp; i \end{vmatrix} = aei + bfg + cdh - ceg - bdi - afh.$$</p>
<p>Now let’s apply the formula to an example:</p>
<p>$$|B| = \begin{vmatrix} 1 &amp; 2 &amp; 3 \ 0 &amp; 4 &amp; 5 \ 1 &amp; 0 &amp; 6 \end{vmatrix} = (1)(4)(6) + (2)(5)(1) + (3)(0)(0) - (3)(4)(1) - (2)(0)(6) - (1)(5)(0)$$</p>
<p>Assessing each term:</p>
<p>$$= (1)(4)(6) + (2)(5)(1) - (3)(4)(1) = 4 \cdot 6 + 2 \cdot 5 - ( 3 \cdot 4) = 24+10-12 = 22$$</p>
<p>In Python code:</p>
<pre><code class="language-plaintext">import numpy as np

# Define the matrix
B = np.array([
    [1, 2, 3],
    [0, 4, 5],
    [1, 0, 6]
])

# Calculate the determinant
det_B = np.linalg.det(B)

print("Determinant of B:", det_B)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756233606615/4e333b35-4714-480a-8a3b-62db799614e1.png" alt="Python code image representing the code above. Finding a 3 by 3 determinant." style="display:block;margin:0 auto" width="2080" height="1564" loading="lazy">

<p>Now, let’s visualize matrix A by plotting its column vectors. Each column will become a vector: (3,1) and (-2,4). This shows us geometrically what the matrix is actually doing.</p>
<p>In a geogebra graph, it gives us this:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756235393476/6b5c38ea-7b27-4e3d-8ad4-346417d35e77.png" alt="Representation of 2 vectors in a Cartesian plane." style="display:block;margin:0 auto" width="1320" height="1003" loading="lazy">

<p>As we can see, the vectors define how each variable influences the system. By visualizing what the matrices are doing, we can find patterns that are harder to find just by looking at formulas.</p>
<p><strong>What does this mean visually?</strong></p>
<p>It means that in the space, this is what our matrix looks like. It’s also how our system of equations is represented.</p>
<p>C1 represents the “force“ or the impact the variable x1 has. And C2 does the same thing for the variable x2.</p>
<p>Now we’ll focus on a 3D matrix example. This matrix D represents a system of three equations with three variables:</p>
<p>$$D = \begin{bmatrix} 2 &amp; -1 &amp; 3 \ 4 &amp; 0 &amp; -2 \ -1 &amp; 5 &amp; 1 \end{bmatrix}$$</p>
<p>$$\begin{align} 2x_1 - x_2 + 3x_3 &amp;= p \ 4x_1 + 0x_2 - 2x_3 &amp;= q \ -x_1 + 5x_2 + x_3 &amp;= r \end{align}$$</p>
<p>Each column can be described as a separate vector:</p>
<p>$$\begin{equation} D = \left[ D_1 \mid D_2 \mid D_3 \right] = \left[ \begin{bmatrix} 2 \ 4 \ -1 \end{bmatrix} \mid \begin{bmatrix} -1 \ 0 \ 5 \end{bmatrix} \mid \begin{bmatrix} 3 \ -2 \ 1 \end{bmatrix} \right] \end{equation}$$</p>
<p>As we can see, D was decomposed in 3 new column vectors:</p>
<p>$$\begin{equation} D_1 = \begin{bmatrix} 2 \ 4 \ -1 \end{bmatrix} \end{equation}$$</p>
<p>and:</p>
<p>$$\begin{equation} D_2 = \begin{bmatrix} -1 \ 0 \ 5 \end{bmatrix} \end{equation}$$</p>
<p>and:</p>
<p>$$\begin{equation} D_3 = \begin{bmatrix} 3 \ -2 \ 1 \end{bmatrix} \end{equation}$$</p>
<p>In a geogebra graph, it gives us this:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756236913078/8d8a3d48-20a9-423b-bfb8-4368d92ec340.png" alt="Representation of 3 vectors in a 3D Cartesian plane." style="display:block;margin:0 auto" width="1525" height="1141" loading="lazy">

<p>In 3D, each vector points in its own direction. Together, they organize three planes. Where all three planes touch is the solution to the system.</p>
<p>This is a key advantage of matrices and linear algebra. They help us visualize both simple and complex systems, enhancing systems thinking and first principles thinking.</p>
<p>The determinant is directly connected to these visualizations. For example, in 2D it measures the area that the vectors stretch over. Now we’ll see how that’s possible.</p>
<p>Let's use matrix A and see what its determinant looks like in geometric terms:</p>
<p>$$A = \begin{bmatrix} 2 &amp; 3 \ 1 &amp; 4 \end{bmatrix}$$</p>
<p>Which can be decomposed into 2 vectors <code>u</code> and <code>v</code>:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756241016899/ded47498-b030-4fa1-a4fe-07153d138a7f.png" alt="Representation of 2 vectors (matrix A) in a Cartesian plane." style="display:block;margin:0 auto" width="859" height="835" loading="lazy">

<p>It gives us this determinant:</p>
<p>$$|A| = \begin{vmatrix} 2 &amp; 3 \ 1 &amp; 4 \end{vmatrix} = (2)(4) - (3)(1) = 8 - 3 = 5.$$</p>
<p>Now let’s see the determinant visually.</p>
<p>From (2,1) and (3,4), we can draw vectors parallel to u and and v. These are called u' and v' and have the same magnitude. They meet at (5,5), and we have a parallelogram that’s completed with these points: (0,0),(2,1),(3,4),(5,5)</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756241586617/d825b8e2-d839-4b15-bdd0-d9b5efd80942.png" alt="Representation of the 4 vectors being used in the determinant" style="display:block;margin:0 auto" width="1063" height="1048" loading="lazy">

<p>The area of the parallelogram is the determinant:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756241692073/deb2e0cd-32a3-4a1a-90e7-e556f5039169.png" alt="Illustrating that the area limited by the 4 vectors is the determinant." style="display:block;margin:0 auto" width="1062" height="976" loading="lazy">

<p>Let’s see another example.</p>
<p>Let’s use a matrix F and see what it truly is:</p>
<p>$$F = \begin{bmatrix} 1 &amp; 2 \ 2 &amp; 4 \end{bmatrix}$$</p>
<p>It gives us this determinant:</p>
<p>$$|F| = \begin{vmatrix} 1 &amp; 2 \ 2 &amp; 4 \end{vmatrix} = (1)(4) - (2)(2) = 4 - 4 = 0$$</p>
<p>In geogebra, we can see that:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756242215981/d88f2e80-04ba-46b9-979d-d7684f161210.png" alt="Representation of the 2 vectors being used in the determinant" style="display:block;margin:0 auto" width="778" height="1072" loading="lazy">

<p>Now let’s try to see the determinant visually:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756242340382/46551578-69a5-4ef9-ab86-9149e7fb4aaa.png" alt="Illustrating that the area limited by the 2 vectors is the determinant and that it does not exist. So the determinant is zero." style="display:block;margin:0 auto" width="721" height="991" loading="lazy">

<p>We can conclude that the area is 0.</p>
<p>Now let’s use a matrix G and see what it truly is:</p>
<p>$$G = \begin{bmatrix} 1 &amp; 5 \ 2 &amp; 3 \end{bmatrix}$$</p>
<p>It gives us this determinant:</p>
<p>$$|G| = \begin{vmatrix} 1 &amp; 5 \ 2 &amp; 3 \end{vmatrix} = (1)(3) - (5)(2) = 3 - 10 = -7$$</p>
<p>In geogebra, we can see that:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756242987960/d182b725-81ba-4042-81e1-6b0232e09ffb.png" alt="Representation of the 2 vectors being used to find the determinant" style="display:block;margin:0 auto" width="1411" height="976" loading="lazy">

<p>Now let’s try to see the determinant visually.</p>
<p>From (1,2) and (5,3), we can draw vectors parallel to u and and v. These are called u' and v' and have the same magnitude. They meet at (6,5). A parallelogram is completed with these points: (0,0),(1,2),(5,3),(6,5)</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756243098714/881693d4-7a84-4b72-bb87-3fb48b25fe4b.png" alt="Representation of 4 vectors being used to find the determinant before showing the area" style="display:block;margin:0 auto" width="1201" height="1030" loading="lazy">

<p>Again, the area of the parallelogram is the determinant:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756243316071/ce8fa65b-6370-4ada-9fe6-cdf20ab4546d.png" alt="Illustrating that the area limited by the 4 vectors is the determinant." style="display:block;margin:0 auto" width="1167" height="1023" loading="lazy">

<p>We just saw that the determinant is the area of a parallelogram formed by the vectors. When the determinant is 0, there is no area. In other cases, there is an area. But what does this mean, and why do we care about these different values?</p>
<p><strong>When the det = 0:</strong></p>
<ul>
<li><p>The vectors are linearly dependent (one can be written as a combination of the others)</p>
</li>
<li><p>They lie on the same line or one is a scaled version of the other</p>
</li>
<li><p>The parallelogram collapses to a line, hence zero area</p>
</li>
<li><p>This tells us the matrix has no inverse</p>
</li>
<li><p><strong>Systems of equations either have no solution or infinitely many solutions</strong></p>
</li>
</ul>
<p><strong>When the det ≠ 0 (det &gt; 0 or det &lt; 0):</strong></p>
<ul>
<li><p>The vectors form a proper parallelogram with an area</p>
<ul>
<li><p>If det &gt; 0, the area is positive and transformation preserves orientation</p>
</li>
<li><p>If det &lt; 0, the area is negative and the orientation is flipped</p>
</li>
</ul>
</li>
<li><p>The vectors are linearly independent</p>
</li>
<li><p><strong>Systems of equations have exactly one solution</strong></p>
</li>
</ul>
<p>In electrical engineering, determinants help verify if a control system is controllable and observable.</p>
<p>Control systems use matrices a lot. For this reason, checking if their determinants are zero or non-zero tells engineers:</p>
<ul>
<li><p>If it is controllable, it means the system is reachable, which helps in stabilization and performance optimization.</p>
</li>
<li><p>If it is observable, it means the system is measurable, which helps in fault detection and system monitoring.</p>
</li>
</ul>
<p>In finite element analysis, a very popular math tool to solve partial differential equations, determinants helps figure out quickly if the calculations will give reliable results.</p>
<p>This way, with finite element analysis, we can design safer buildings, optimize aircraft wings, and simulate medical implants – all of which have a large impact on human lives and safety.</p>
<p>In machine learning, determinants are crucial to understanding data transformations. In these methods, if a determinant with a value of zero shows up, it means you are losing information and can't recover original data.</p>
<p>Also in deep learning, it’s used to decide the first parameters of neural networks (weight initialization) to prevent problems like the vanishing/exploding gradients.</p>
<p>In a 3×3 matrix, the determinant represents the volume of a parallelepiped (a 3D "box") formed by three vectors in 3D space.</p>
<ul>
<li><p>If det = 0: The three vectors lie in the same plane, so they don't span any 3D volume</p>
</li>
<li><p>If det ≠ 0: The vectors form a proper 3D shape with actual volume</p>
</li>
</ul>
<p>The absolute value |det| gives you the exact volume of that <a href="https://en.wikipedia.org/wiki/Parallelepiped">parallelepiped</a>.</p>
<p>For example, if you have vectors a, b, and c, the determinant tells you how much 3D space they "fill up" when you use them as the edges of a box.</p>
<p>This is where it gets fascinating:</p>
<ul>
<li><p>4×4 matrix: The determinant represents the "hypervolume" of a 4D parallelepiped formed by four vectors in 4-dimensional space.</p>
</li>
<li><p>1000×1000 matrix: The determinant represents the hypervolume in 1000-dimensional space!</p>
</li>
</ul>
<p>So, to summarize, the determinant tells us easily if there are no solutions, infinite solutions, or exactly one solution in a system of equations, represented by a compact matrix.</p>
<h3 id="heading-what-are-mathematical-spaces-and-how-do-they-simplify-calculations">What Are Mathematical Spaces and How Do They Simplify Calculations?</h3>
<p>We now have a great foundation to understand the rest of this chapter on linear algebra.</p>
<p>Now, we will see see how a linearly independent matrix create something called a basis. Also, we will see that a basis is just a a set of building blocks for mathematical spaces!</p>
<p>The row vectors of a linearly independent matrix form a basis.</p>
<p>For example in matrix A, which is linearly independent:</p>
<p>$$A = \begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \ 0 &amp; 1 &amp; 0 &amp; 0 \ 0 &amp; 0 &amp; 1 &amp; 0 \ 0 &amp; 0 &amp; 0 &amp; 1 \end{bmatrix}$$</p>
<p>forms this set:</p>
<p>$$((1,0,0,0), (0,1,0,0), (0,0,1,0), (0,0,0,1))$$</p>
<p>In this case, since matrix A is linearly independent, the set of matrix rows is called a <strong>basis</strong>. From this basis, you can create endless linear combinations of any other vector. The collection of all these possible combinations is called a <strong>mathematical space</strong>.</p>
<p>A mathematical space is an infinite set where all linear combinations of a basis exist. Its called a basis because these vectors <strong>form the base</strong> to express any vector in the space as a linear combination.</p>
<p>This matrix B is linearly independent:</p>
<p>$$B = \begin{bmatrix} 1 &amp; 0 \ 0 &amp; 1 \ \end{bmatrix}$$</p>
<p>And forms this set:</p>
<p>$$((1, 0), (0, 1))$$</p>
<p>And from this come all possible points in this cartesian coordinate system:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756247201687/a847b8c0-5678-431c-b446-e1897afdffc6.png" alt="Showing in the Cartesian plane where the point (2, 3) is" style="display:block;margin:0 auto" width="1084" height="1114" loading="lazy">

<p>For example, mathematically, we can get the point (2,3) by:</p>
<p>$$(x=2, y=3) = 2(1, 0) + 3(0, 1) = (2, 0) + (0, 3) = (2, 3)$$</p>
<p>Note: There are other bases for the cartesian coordinate plane. I chose this one because it’s the easiest to understand.</p>
<h3 id="heading-eigenvalues-and-eigenvectors-unlocking-hidden-patterns">Eigenvalues and Eigenvectors: Unlocking Hidden Patterns</h3>
<p>Eigenvalues and eigenvectors, in my opinion, are far simpler than what mathematics professors make them out to be at university:</p>
<ul>
<li><p>Eigenvalues tell you how much a matrix stretches or shrinks things.</p>
</li>
<li><p>Eigenvectors tell you which directions stay unchanged when the matrix transforms them.</p>
</li>
</ul>
<p>This way, a matrix may have one or many eigenvalues which in turn result in many eigenvectors.</p>
<p>Let’s see an example:</p>
<p>For a square matrix A, eigenvalue λ, and eigenvector v:</p>
<p>$$Av=λv$$</p>
<p>The easiest way to find the eigenvalue is to calculate this:</p>
<p>$$det(A−λI)=0$$</p>
<p>or:</p>
<p>$$|A−λI|=0$$</p>
<p>Again, we have different notations for the determinant, but they’re the same thing.</p>
<p>Anyway, let’s define a very simple matrix A:</p>
<p>$$A = \begin{bmatrix} 2 &amp; 0 \ 0 &amp; 3 \end{bmatrix}$$</p>
<p>Now let’s make some calculations.</p>
<p>This formula:</p>
<p>$$det(A−λI)=0$$</p>
<p>Can be decomposed into:</p>
<p>$$det(\begin{bmatrix} 2 &amp; 0 \ 0 &amp; 3 \end{bmatrix} - λ \times \begin{bmatrix} 1 &amp; 0 \ 0 &amp; 1 \end{bmatrix}) = 0$$</p>
<p>Which is the same has:</p>
<p>$$det(\begin{bmatrix} 2 &amp; 0 \ 0 &amp; 3 \end{bmatrix} - \begin{bmatrix} λ &amp; 0 \ 0 &amp; λ \end{bmatrix}) = 0$$</p>
<p>Which gives us:</p>
<p>$$det(\begin{bmatrix} 2-λ &amp; 0 \ 0 &amp; 3-λ \end{bmatrix}) = 0$$</p>
<p>By the calculations we made above on the determinant, we can conclude that:</p>
<p>$$(2-λ) \times (3-λ) = 0$$</p>
<p>Which is the same has:</p>
<p>$$2-\lambda = 0 \text{ or } 3-\lambda = 0$$</p>
<p>Which gives us these eigenvalues:</p>
<p>$$\lambda_1 = 2, \quad \lambda_2 = 3$$</p>
<p>And these eigenvectors:</p>
<p>$$\mathbf{v_1} = \begin{bmatrix} 1 \ 0 \end{bmatrix}, \quad \mathbf{v_2} = \begin{bmatrix} 0 \ 1 \end{bmatrix}$$</p>
<p>This means that in the Cartesian coordinate system:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756321668969/949a5a4b-12ff-4490-bbff-1cc032bc5705.png" alt="Showing how the eigenvectors are related to the vectors in matrix A visually. Both have the same directions but different scalar values." style="display:block;margin:0 auto" width="997" height="988" loading="lazy">

<p>By applying the eigenvectors, we can see that:</p>
<ul>
<li>The eigenvalue 2 is associated with the eigenvector v1:</li>
</ul>
<p>$$A\mathbf{v_1} = \begin{bmatrix} 2 &amp; 0 \ 0 &amp; 3 \end{bmatrix}\begin{bmatrix} 1 \ 0 \end{bmatrix} = \begin{bmatrix} 2 \ 0 \end{bmatrix} = 2\begin{bmatrix} 1 \ 0 \end{bmatrix}$$</p>
<ul>
<li>The eigenvalue 3 is associated with the eigenvector v2:</li>
</ul>
<p>$$A\mathbf{v_2} = \begin{bmatrix} 2 &amp; 0 \ 0 &amp; 3 \end{bmatrix}\begin{bmatrix} 0 \ 1 \end{bmatrix} = \begin{bmatrix} 0 \ 3 \end{bmatrix} = 3\begin{bmatrix} 0 \ 1 \end{bmatrix}$$</p>
<p>Here is the Python code to calculate this:</p>
<pre><code class="language-plaintext">import numpy as np

# Define matrix A
A = np.array([[2, 0],
              [0, 3]])

# Calculate eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)

print("Eigenvalues:")
print(eigenvalues)

print("Eigenvectors (columns):")
print(eigenvectors)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756322044095/bc76f0ec-1d13-4845-b0f3-2847118860a3.png" alt="Python code, with NumPy array, showing how to find the eigenvalues" style="display:block;margin:0 auto" width="2080" height="1744" loading="lazy">

<p>Eigenvalues and eigenvectors are key tools in engineering and machine learning because they reveal a matrix's fundamental behavior. Although a matrix transformation might seem complex, in reality:</p>
<ul>
<li><p>Eigenvalues show how much stretching or compression occur.</p>
</li>
<li><p>Eigenvectors identify the special directions where this stretching happens most naturally.</p>
</li>
</ul>
<p>In machine learning, we can use Principal Component Analysis (PCA) to make datasets smaller.</p>
<p>So, for example, let's say you’re building a machine learning application to predict heart disease. You have 100 data categories and 1 target variable telling whether a person has it or not.</p>
<p>With PCA, you can convert the 100 categories into, say, 40 categories. This way, you can make a smaller machine learning model and save computational resources.</p>
<p>PCA uses eigenvectors of covariance matrices to find important directions in data with many variables. It reduces data size without losing much detail, helping machine learning algorithms focus on key features and ignore unnecessary information.</p>
<h3 id="heading-applications-of-linear-algebra-in-ai-and-control-theory">Applications of Linear Algebra in AI and Control Theory</h3>
<p>‌Linear algebra serves as the mathematical foundation for all engineering fields.</p>
<p>In addition, the principles of matrices and linear transformations provide the computational foundation that makes modern AI possible while enabling the control of complex systems.</p>
<p>All LLMs, from ChatGPT and Claude to Gemini and Grok, rely on linear operations.</p>
<p>All these systems carry out huge matrix multiplications to handle and create human language. So, when you type something into ChatGPT, probably millions of matrix multiplications are happening as you wait for a response!</p>
<p>In control theory, especially in an area called state-space control theory, matrices make it possible to create complex controllers. Linear algebra helps engineers design controllers for things like aircraft autopilots and robotic systems, among other applications</p>
<p>For example, when a rocket adjusts its trajectory or a drone maintains stable flight, many matrix multiplications are happening to determine the best way to guarantee the system’s stability.</p>
<p>Thanks to GPUs, linear algebra matrices are very efficient to compute. Also, any new matrix multiplication algorithms or special hardware for faster linear operations can greatly enhance AI and control systems.</p>
<p>In the end, linear algebra is the hidden mathematical engine powering the current AI revolution.</p>
<h2 id="heading-chapter-5-multivariable-calculus-change-in-many-directions">Chapter 5: Multivariable Calculus -&nbsp;Change in Many Directions</h2>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765002238157/a377cdc6-7e85-491b-90b8-8b3243618288.jpeg" alt="Photo of a women writing a calculus equation in a board" style="display:block;margin:0 auto" width="7804" height="5205" loading="lazy">

<p><a href="https://www.pexels.com/photo/woman-writing-on-a-whiteboard-3862130/">Photo by ThisIsEngineering</a></p>
<h3 id="heading-limits-and-continuity-understanding-smooth-change">Limits and Continuity: Understanding Smooth Change</h3>
<p>Calculus is one of the most valuable areas of mathematics and it focus on the study of continuous change.</p>
<p>Before we start learning a topic that makes many people give up on engineering degrees, I want to once again assure you that this chapter is very easily explained with a lot of images and code examples.</p>
<p>Also, just like linear algebra, many concepts in calculus are components of tools that have helped create billion-dollar industries.</p>
<h4 id="heading-what-is-continuity">What is continuity?</h4>
<p>Before going and explaining topics like derivatives and integrals, we need to understand continuity.</p>
<p>In simple terms, continuity means that a function has no breaks, jumps, or holes.</p>
<p>Essentially, you can draw it without lifting your pencil from the paper.</p>
<p>For example, this function is continuous:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756402257225/f9cfc4f3-a6f1-4fb9-9ed1-f690c4ffffc4.png" alt="Example of a function that is continuous" style="display:block;margin:0 auto" width="634" height="901" loading="lazy">

<p>You can draw this graph without taking the pencil off the paper.</p>
<p>The above graph is represented by this function:</p>
<p>$$y = x^2 - 4x + 3$$</p>
<p>But the below function is <strong>not</strong> continuous:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756402337970/b5a65748-572d-4342-9685-9472babde38a.png" alt="Example of a function that is not continuous" style="display:block;margin:0 auto" width="1315" height="1084" loading="lazy">

<p>This one, you <strong>can’t</strong> draw without taking the pencil off the paper.</p>
<p>It’s represented by this piecewise function:</p>
<p>$$y = \begin{cases} 1.5 + \frac{1}{x+1} &amp; \text{if } -1 &lt; x &lt; 2 \ 2 + \frac{2}{(x-1)^2} &amp; \text{if } x &gt; 2 \end{cases}$$</p>
<p>This piecewise function is essentially two individual functions for two different intervals of numbers. Since calculus is the study of continuous change, we can only realistically use it in continuous functions.</p>
<h4 id="heading-how-do-limits-guarantee-continuity">How do limits guarantee continuity?</h4>
<p>We can only use tools like derivatives and integrals if a function is continuous.</p>
<p>How can we describe mathematically that a function is continuous – like drawing it without lifting our pencil from the paper?</p>
<p>Limits solve that problem.</p>
<p>When we take the limit of a function at a given point, we're asking: what value does a function approach as we get close to that point?</p>
<p>Let's look at some examples of this function at these points and also understand the notation used in limits:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756403511442/de3450f2-dcf9-40e3-a04e-846334abeebd.png" alt="Example of a function that is continuous and its various points" style="display:block;margin:0 auto" width="759" height="1104" loading="lazy">

<ol>
<li><strong>What is the limit of the point x=0?</strong></li>
</ol>
<p>It is 3. It actually crosses the y axis.</p>
<p>In mathematical notation,</p>
<p>$$\begin{align} \lim_{x \to 0} (x^2 - 4x + 3) &amp;= (0)^2 - 4(0) + 3 \ &amp;= 0 - 0 + 3 \ &amp;= 3 \end{align}$$</p>
<p>In this notation, we're asking what the value of the y function is as x gets very close to 0. Think of x as being at 0.00000000000001 or -0.00000000000001. It gets so close that we can consider it near enough.</p>
<ol>
<li><strong>What is the limit of the point x=1?</strong></li>
</ol>
<p>Le’s see another example:</p>
<p>In this case, it’s 0.</p>
<p>$$\begin{align} \lim_{x \to 1} (x^2 - 4x + 3) &amp;= (1)^2 - 4(1) + 3 \ &amp;= 1 - 4 + 3 \ &amp;= 0 \end{align}$$</p>
<p>In this notation, we're asking what the value of the y function is as x gets very close to 1. Think of x as being at 0.99999999999999 or 1.00000000000001. It gets so close that we can consider it near enough.</p>
<ol>
<li><strong>What is the limit of the point x=2?</strong></li>
</ol>
<p>Le’s see another example</p>
<p>Here, it’s -1.</p>
<p>$$\begin{align} \lim_{x \to 2} (x^2 - 4x + 3) &amp;= (2)^2 - 4(2) + 3 \ &amp;= 4 - 8 + 3 \ &amp;= -1 \end{align}$$</p>
<p>Some more quick examples:</p>
<ol>
<li><strong>What is the limit of the point x=3?</strong></li>
</ol>
<p>In this notation, we're asking what the value of the y function is as x gets very close to 1. Think of x as being at 1.99999999999999 or 2.00000000000001. It gets so close that we can consider it near enough.</p>
<ol>
<li><strong>What is the limit of the point x=4?</strong></li>
</ol>
<p>It is 0.</p>
<ol>
<li><strong>What is the limit of the point x=5?</strong></li>
</ol>
<p>It is 3.</p>
<p>Now let’s see another example:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756403617161/b67b2977-8ae4-4c06-8156-d7c6a64ee2e1.png" alt="Example of a function that is not continuous at a point of x=2" style="display:block;margin:0 auto" width="1315" height="1084" loading="lazy">

<p>In the point x=2, it’s not well defined</p>
<ul>
<li><p>If we draw with a pencil from the left to x=2, we end up with 1.83333</p>
</li>
<li><p>If we draw with a pencil from the right to x=2, we end up with 4</p>
</li>
</ul>
<h3 id="heading-why-are-limits-important-to-understand-derivatives-and-integrals">Why are limits important to understand derivatives and integrals?</h3>
<p>As we have seen, when we talk about limits, we are talking about a value that symbolizes the value that a function approaches as it comes toward a particular point.</p>
<p>It’s critical to note that we're not looking at the value of that point itself. We’re looking at what happens as we get so near to it that we can pin down what value the function is approaching.</p>
<p>I will now show a very simple example to demonstrate this concept using mathematical notation.</p>
<p>I know that limits can be a difficult concept to understand at first. But if you understand limits very well, then you'll be well-prepared to understand derivatives and integrals.</p>
<p>And, as you’ll see, derivatives are responsible for modern AI and integrals are important parts of tolls widely used in billion-dollar industries.</p>
<p>I want you to understand the <strong>intuition</strong> behind this.</p>
<p>The function z(x) is continuous:</p>
<p>$$z(x) = \frac{3x + 7}{x + 2}$$</p>
<p><strong>So to what value does this expression converge as x approaches infinity?</strong></p>
<p>If you have a background in math, you might see why. But here for those who aren’t sure:</p>
<ul>
<li>It converges to 3.</li>
</ul>
<p>This time, the limit will be approaching infinity instead of a constant:</p>
<p>$$\begin{align} \lim_{x \to \infty} \frac{3x + 7}{x + 2} \end{align}$$</p>
<p>Let’s solve this in a very simple way:</p>
<ul>
<li>For x = 1:</li>
</ul>
<p>$$f(1) = \frac{3(1) + 7}{1 + 2} = \frac{10}{3} \approx 3.333...$$</p>
<ul>
<li>For x = 5:</li>
</ul>
<p>$$f(5) = \frac{3(5) + 7}{5 + 2} = \frac{22}{7} \approx 3.143...$$</p>
<ul>
<li>For x = 10:</li>
</ul>
<p>$$f(10) = \frac{3(10) + 7}{10 + 2} = \frac{37}{12} \approx 3.083...$$</p>
<ul>
<li>For x = 50:</li>
</ul>
<p>$$f(50) = \frac{3(50) + 7}{50 + 2} = \frac{157}{52} \approx 3.019...$$</p>
<ul>
<li>For x = 100:</li>
</ul>
<p>$$f(100) = \frac{3(100) + 7}{100 + 2} = \frac{307}{102} \approx 3.010...$$</p>
<ul>
<li>For x = 1000:</li>
</ul>
<p>$$f(1000) = \frac{3(1000) + 7}{1000 + 2} = \frac{3007}{1002} \approx 3.001...$$</p>
<ul>
<li>For x = 10000:</li>
</ul>
<p>$$f(10000) = \frac{3(10000) + 7}{10000 + 2} = \frac{30007}{10002} \approx 3.0001...$$</p>
<p>As x gets bigger and bigger, we get closer and closer to 3.</p>
<p>This is the main idea of limits: Describe the value a function approaches as the input approaches some point.</p>
<p>This same idea applies to derivatives: they’re just limits that measure rates of change (slopes of tangent lines).</p>
<p>And as well, Integrals are just limits that measure accumulated quantities (areas under curves)..</p>
<p>Let’s now see how derivatives work in depth.</p>
<h3 id="heading-derivatives-how-things-change-and-how-fast">Derivatives: How Things Change and How Fast</h3>
<p>As I said before, derivatives are just limits that measure rates of change (slopes of tangent lines).</p>
<p>But what does this actually mean?</p>
<p>Let’s see an example:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756755419750/75b36254-0f4a-4395-8dd4-14ac16399ff3.png" alt="Example of a function" style="display:block;margin:0 auto" width="1263" height="1005" loading="lazy">

<p><strong>What is the rate of change in the point A?</strong></p>
<p>Hard question right? Let’s think how to answer this with limits.</p>
<p>We can find the limit of the rate of change in point A(0.72, 0.66), also called the instantaneous rate of change.</p>
<p>Let’s do that:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756755680672/40f94361-55c7-4a9e-bfaf-b2b855fa0712.png" alt="Example of a function and choosing two points (B and C) to find the rate of change in point A" style="display:block;margin:0 auto" width="1437" height="957" loading="lazy">

<p>To find the slope, we take the coordinates of the points B(0.2, 0.2) and C(1.6, 1):</p>
<p>$$\text{slope} = \frac{1 - 0.2}{1.6 - 0.2} = \frac{0.8}{1.4} = \frac{4}{7} \approx 0.571$$</p>
<p>This gives us a rate of change:</p>
<p>$$y=0.571x + 0.084$$</p>
<p>Let's approximate more:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756756069833/3a4a1991-4983-4751-a68e-68bd6780300d.png" alt="Example of a function and choosing two points (B and C) to find the rate of change in point A. But B and C are closer to A." style="display:block;margin:0 auto" width="1492" height="1027" loading="lazy">

<p>Let’s also zoom in:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756756131072/f96b7f82-a4ed-4720-8c87-fd2936bae9d9.png" alt="Example of a function and choosing two points (B and C) to find the rate of change in point A. But B and C are closer to A, and we have to zoom in." style="display:block;margin:0 auto" width="1569" height="1134" loading="lazy">

<p>To find the slope, we use the coordinates of the points B(0.58, 0.55) and C(0.85, 0.75):</p>
<p>$$\text{slope} = \frac{0.85- 0.58}{0.75 - 0.55} = \frac{0.27}{0.2} = \frac{2.7}{2} \approx 1.35$$</p>
<p>It gives us a rate of change:</p>
<p>$$y=1.35x + 0.11$$</p>
<p>Now let's approximate a lot:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756756879223/11d26af3-06ec-4419-b631-10308b4cadef.png" alt="Example of a function and choosing two points (B and C) to find the rate of change in point A. But B and C are closer to A, and we have to zoom in." style="display:block;margin:0 auto" width="1513" height="1098" loading="lazy">

<p>To find the slope, we use the coordinates of the points B(0.7242549, 0.6625776) and C(0.7242884, 0.66260026):</p>
<p>$$\text{slope} = \frac{0.66260026- 0.6625776}{0.7242884- 0.7242549} = \frac{0.0000226}{0.0000335} = \frac{0.226}{0.335} \approx 0.674$$</p>
<p>Now let’s zoom out:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756757322888/a6f58b41-d6ff-44fd-b18f-06fb1f8f0e06.png" alt="Rate of change at point C" style="display:block;margin:0 auto" width="1195" height="907" loading="lazy">

<p>As we can see, we are so close that we can consider the limit of the rate of change to be 0.65.</p>
<p>It gives us the rate of change:</p>
<p>$$y=0.674x + 0.12$$</p>
<p><strong>This way, the limit of a rate of change is called a derivative.</strong></p>
<p>To recap, here is an animation:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756766733257/a1754b47-7c57-4387-8b4c-886ed7b8f80a.gif" alt="GIF animation based on previous images" style="display:block;margin:0 auto" width="1195" height="907" loading="lazy">

<p>Here’s a Python code example that lets you find the derivative in point A:</p>
<pre><code class="language-python">import sympy as sp

x = sp.symbols('x')
f = sp.sin(x)

# Derivative of sin(x)
derivative_of_sin = sp.diff(f, x)

# Evaluate at x = 0.72 and x = 0.66
val = f_prime.subs(x, 0.72).evalf()

print("Derivative of sin(x) at x=0.72:", val)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756758436107/3bda58c5-96d6-4834-a2ec-ab8fedc4cb56.png" alt="Image of code example to find the derivative of the function sin(x)" style="display:block;margin:0 auto" width="2080" height="1564" loading="lazy">

<p>The function that had the point A is called a sine wave.</p>
<p>We convert it to its derivative function. From there we have our rate of change at point 0.72.</p>
<p>When we do math by hand, <strong>we usually have many rules to convert a function to its derivative, and from these find the rate of change for a given point.</strong></p>
<p>Before seeing it, let’s look at a very simple example to understand the definition of a derivative:</p>
<p>$$\frac{d}{dx}f(x) \approx \frac{f(\textcolor{green}{x + h}) - f(\textcolor{red}{x - h})}{\textcolor{green}{x + h} - \textcolor{red}{x - h}} = \frac{f({x + h}) - f({x - h})}{2h}$$</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1756767749954/87486d8c-9437-460c-b556-e9333b1590c5.png" alt="Image showing in derivative definition how each component is related visually to a line representing the rate of change" style="display:block;margin:0 auto" width="1513" height="1098" loading="lazy">

<p><code>h</code> represents a small difference.</p>
<p>The derivative is the slope of the function’s small change near a point. In other words, it’s the limit of the rate of change of a given point.</p>
<p>A simple derivative transformation might look like this one:</p>
<p>$$\frac{d}{dx}x^n = nx^{n-1}$$</p>
<p>Two examples are:</p>
<p>$$\frac{d}{dx}x^3 = 3x^2$$</p>
<p>And:</p>
<p>$$\frac{d}{dx}x^5 = 5x^4$$</p>
<p>There are many more. But we won’t go into deep detail on this topic.</p>
<h4 id="heading-where-and-why-are-derivatives-so-important">Where and why are derivatives so important?</h4>
<p>Derivatives are one of the most important math tools out there. They serve as the foundation for understanding change across nearly all fields of STEM.</p>
<p>In physics (classical mechanics), derivatives are very important to find new information that draws on information that’s already made available.</p>
<p>For example, knowing how a body's position changes over time allows us to use derivatives to find its velocity and acceleration. This is crucial for self-driving cars, trains, rockets, and more.</p>
<p>Also, derivatives are the foundation of understanding how electricity works in depth. Without derivatives, there would’ve been no electromagnetic theory. Without electromagnetic theory, modern technology would not exist.</p>
<p>In machine learning, derivatives are so important that they served to create the algorithm that is one of the most important components of ChatGPT and others AI models. (backpropagation).</p>
<p>Backpropagation is in fact so important that its creators, John Hopfield and Geoffrey Hinton, won the 2024 Nobel Prize in Physics for it.</p>
<p>Also, autonomous vehicles like Tesla and Waymo use AI models called neural networks that depend on backpropagation to work.</p>
<p>It’s awesome that a math concept created in the 17th century is now one of the foundations of the current AI revolution.</p>
<h3 id="heading-what-about-integral-calculus">What About Integral Calculus?</h3>
<p>Before explaining derivatives further, I will ask you a question:</p>
<p>How can we find the area of the below shape?</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764401826343/2583b3b0-0bcd-4204-921e-300b27c9fc3d.png" alt="Image of a finite integral of the function sin(x)" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>In other words how can we find the integral of the function in the given interval?</p>
<p>Let’s see how to do it step by step.</p>
<p>First, we’ll try using 2 rectangles to approximate the area behind the curve:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764402058848/5023772e-ed0d-4efc-a5cd-3e1a856f6d69.png" alt="Using 2 rectangles to try to find the area under the curve" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Now the area of the rectangles is 6.282573.</p>
<p>But there is still a lot of error…</p>
<p>As we can see, the left rectangle does not cover completely the curve and the right rectangle covers too much.</p>
<p>So we’ll add more smaller rectangles so that we can better approximate the curve.</p>
<p>Now let’s try using 4 rectangles:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764483444354/c06cd1c2-0f92-4728-898e-fbaf1534d57f.png" alt="Using 4 rectangles to try to find the area under the curve" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Now the area is 6.497481. But there’s still some error.</p>
<p>As we can see, the error is getting smaller. In other words, the 4 rectangles cover the area of the curve better than just the 2 rectangles. But there’s still a lot of room to make it better.</p>
<p>Let’s try using 8 rectangles:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764402069389/e9ad0576-dd9d-4535-bf3a-4c4bcd77db98.png" alt="Using 8 rectangles to try to find the area under the curve" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Now the area is 6.604935.</p>
<p>How about using 16 rectangles?</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764402075078/6ad6278f-4b71-411b-8552-2554152a04cb.png" alt="Using 16 rectangles to try to find the area under the curve" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Now the area is 6.658662.</p>
<p>Let’s try using 32 rectangles:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764402079649/4e673391-7e7a-4ca3-b07a-22508c5b058e.png" alt="Using 32 rectangles to try to find the area under the curve" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Now the area is 6.685525.</p>
<p>Now how about using 64 rectangles:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764402084920/4851d710-ff9d-4562-ba7d-9b759473f577.png" alt="Using 64 rectangles to try to find the area under the curve" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Now the area is 6.698957.</p>
<p>And using 128 rectangles:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764402090280/bd5b139c-58e1-4a7a-869d-5107b7eff345.png" alt="Using 128 rectangles to try to find the area under the curve" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Now the area is 6.705673.</p>
<p>What about using 256 rectangles:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764402098061/3ee50020-0143-42b1-aea7-8c762aa33e53.png" alt="Using 256 rectangles to try to find the area under the curve" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Now the area is 6.709031. And the error has reached 0.0000!</p>
<p>Now let’s see an animation of this:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764402052869/e9a54332-75b5-4e46-90cc-3bc09e636ad3.gif" alt="GIF animation of the rectangles from 2 to 256 to represent the finite integral" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>As you can see, we can approximate the area by having a limit to infinity to the number of rectangles to approximate the area.</p>
<p>This way, we can conclude that:</p>
<p>$$F(x) = \int_0^{3.14} f(x) , dx = \int_0^{3.14} (\sin(x) + 1.5) , dx = 6.71$$</p>
<p>This means that the area between 0 and 3.14, limited by the math equation, is 6.71!</p>
<p>Or, mathematically, the integral of f(x) in the interval 0 and 3.14 is 6.71.</p>
<h4 id="heading-where-and-how-is-this-applied">Where and how is this applied?</h4>
<p>In electrical engineering, integrals calculate total energy use in circuits by integrating power over time. For example, when designing a power supply for a device, engineers integrate the power to determine total energy costs and heat absorption requirements.</p>
<p>In other words, they see the area over time and how much power is used.</p>
<p>Let's see an example:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764832775180/911672dd-05ff-47c7-ac5f-81f4933c96ff.png" alt="Image of integral" style="display:block;margin:0 auto" width="1500" height="900" loading="lazy">

<p>Imagine that in the image above:</p>
<ul>
<li><p>The X axis can be the time in months.</p>
</li>
<li><p>The Y axis is the power used in Watts (Joules per second).</p>
</li>
</ul>
<p>We can conclude that in 3.14 months(3 months and 4 days) the total amount of energy is 6.71 watt-months.</p>
<p>Here is the code to find that out:</p>
<pre><code class="language-plaintext"># Import libraries
import numpy as np
import matplotlib.pyplot as plt

# Create Function
x = np.linspace(0, 3.14, 100)
y = np.sin(x) + 1.5

# Find the area under the function
area = np.trapezoid(y, x)

# Show the final image
plt.fill_between(x, y)
plt.title(f'Area = {area:.2f}')
plt.show()
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765435075995/defc251b-812c-44ae-8b67-9a323c0af040.png" alt="Code to find finite integral of the function sin between two points" style="display:block;margin:0 auto" width="2080" height="1384" loading="lazy">

<p>In this code, we import the libraries, create the function, and find the area and plot it.</p>
<p>We used numpy.trapezoid to find the area, because it’s a numerical approximation to quickly find the integral of a function between two x values.</p>
<p>numpy.trapezoid uses a numerical approximation method called the <strong>composite trapezoidal rule.</strong></p>
<p>The basic idea of the composite trapezoidal rule is to divide the area under the curve into many trapezoids and sum all of them.</p>
<p>If you want to learn more about this, I recommend reading the <a href="https://numpy.org/doc/stable/reference/generated/numpy.trapezoid.html">NumPy documentation on this method</a>.</p>
<p>From this value, we can convert to other units:</p>
<ul>
<li><p>52,400,000 joules</p>
</li>
<li><p>14.6 kWh</p>
</li>
</ul>
<p>By converting to other units, we can more easily compare this device with other devices and see if it obeys any technical standards and laws.</p>
<p><strong>This is a real-life application of integrals in engineering.</strong></p>
<p>In my degree, I used this a lot in classes related to power engineering. In simple words, power engineering is a subfield of electrical engineering focused on working with electricity with very high voltage values and electric motors.</p>
<p>In audio compression, the Fourier transform (built on integrals) decomposes sound waves into frequency components. MP3 encoders use this to identify and remove frequencies humans can't hear. This reduces file sizes while preserving quality.</p>
<p>Medical imaging relies on the Radon transform, which uses integrals to reconstruct 3D images from 2D X-ray projections. When you get a CT scan, the machine takes hundreds of X-ray "slices" at different angles. During this process, integrals combine "slices" into a detailed cross-sectional image of your body.</p>
<h3 id="heading-applications-in-ai-and-control-theory-calculus-in-action">Applications in AI and Control Theory: Calculus in Action</h3>
<p>Modern AI depends on derivatives that use the backpropagation algorithm.</p>
<p>When training a neural network, the system calculates partial derivatives of the error with respect to millions of parameters. This way, find out how to adjust each weight to improve performance. Without this, large language models like ChatGPT couldn't learn from data.</p>
<p>PID controllers, which stabilize the temperature in your oven or maintain altitude in aircraft autopilot systems, combine calculus ideas:</p>
<ul>
<li><p>The proportional term responds to the current error.</p>
</li>
<li><p>The integral term accumulates past errors to eliminate steady-state drift.</p>
</li>
<li><p>The derivative term predicts future trends to prevent overshooting.</p>
</li>
</ul>
<p>And these are just some of the applications of calculus!</p>
<h2 id="heading-chapter-6-probability-amp-statistics-learning-from-uncertainty">Chapter 6: Probability &amp; Statistics - Learning from Uncertainty</h2>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765002445093/b606e188-969e-49d8-9be9-9c15330a2939.jpeg" alt="Many purple dice together" style="display:block;margin:0 auto" width="6016" height="4000" loading="lazy">

<p><a href="https://www.pexels.com/photo/purple-dices-with-different-geometrical-shape-on-a-white-surface-3649115/">Photo by Armando Are</a></p>
<p>It’s thanks to probabilities and statistics that many industries have grown so much. With statistics, we can make informed decisions and optimize many different processes. With probabilities, we can understand and model uncertainty in systems and, in this way, solve or even avoid problems.</p>
<p>While you may be familiar with some of the key concepts like median and mean, we’ll start with some basics to build up your intuition on more advanced stuff like the central limit theorem, Bayes’ theorem, and Markov chains.</p>
<h3 id="heading-mean-median-mode-measuring-central-tendency">Mean, Median, Mode: Measuring Central Tendency</h3>
<p>Let's imagine you are a data scientist working in research. You’re going to work with data to optimize the output of farms in the Central Valley in California.</p>
<p>The idea is to take in a bunch of data, and by studying it, you can help farmers make better decisions.</p>
<p>Here’s the data from one year of activity:</p>
<table>
<thead>
<tr>
<th>Farm</th>
<th>Yield (tons/ha)</th>
<th>Fertilizer Used (kg/ha)</th>
<th>Rainfall (mm)</th>
</tr>
</thead>
<tbody><tr>
<td>A</td>
<td>4.2</td>
<td>150</td>
<td>280</td>
</tr>
<tr>
<td>B</td>
<td>5.8</td>
<td>220</td>
<td>420</td>
</tr>
<tr>
<td>C</td>
<td>3.9</td>
<td>120</td>
<td>230</td>
</tr>
<tr>
<td>D</td>
<td>6.1</td>
<td>250</td>
<td>480</td>
</tr>
<tr>
<td>E</td>
<td>4.7</td>
<td>200</td>
<td>340</td>
</tr>
<tr>
<td>F</td>
<td>5.3</td>
<td>200</td>
<td>390</td>
</tr>
</tbody></table>
<p>We have 6 farms in our dataset. For each farm, we know:</p>
<ul>
<li><p>How much yield was obtained in tons per hectare</p>
</li>
<li><p>How much fertilizer was used in kilograms per hectare</p>
</li>
<li><p>How much rainfall happened during a year of activity</p>
</li>
</ul>
<p>Now, let’s answer some questions we might have about the data to understand the <strong>mean</strong>, <strong>mode</strong> and <strong>median</strong>:</p>
<h4 id="heading-1-what-is-the-average-yield-during-one-year-of-activity">1. What is the average yield during one year of activity?</h4>
<p>To find the average, we just need to sum all the yield values and divide by the number of farms. Like this:</p>
<p>$$\text{Mean} = \frac{4.2 + 5.8 + 3.9 + 6.1 + 4.7 + 5.3}{6} = \frac{30}{6} = 5$$</p>
<p>This is what is called the mean. The mean is just the sum of all values divided by how many values there are.</p>
<p>In Python, we can do the following to calculate the mean:</p>
<pre><code class="language-plaintext">def calculate_mean(values):
    return sum(values) / len(values)

# Example usage
data = [4.2, 5.8, 3.9, 6.1, 4.7, 5.3]
result = calculate_mean(data)
print(f"Mean: {result}")
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763102054838/b5619d92-95ca-4c50-bb32-39d6e8e7ba7b.png" alt="Python code in an image showing how to find the mean" style="display:block;margin:0 auto" width="2080" height="1024" loading="lazy">

<h4 id="heading-2-what-is-the-mode-of-fertilizer-used">2. What is the mode of fertilizer used?</h4>
<p>The mode is just the most popular value in a given dataset. In our case, it’s <strong>200</strong> since that’s the most common value that appears in our farm dataset.</p>
<p>In Python, we can do this to calculate the mode:</p>
<pre><code class="language-plaintext">import statistics

def calculate_mode(values):
    return statistics.mode(values)

# Example usage
data = [150, 220, 120, 250, 200, 200]
result = calculate_mode(data)
print(f"Mode: {result}")
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763102576660/3ca71e03-f762-44ad-85c3-8ccb4cb1db54.png" alt="Python code in an image showing how to find the mode" style="display:block;margin:0 auto" width="2080" height="1204" loading="lazy">

<h4 id="heading-3-what-is-the-median-of-the-yield">3. What is the median of the yield?</h4>
<p>The median is just the value in the middle of a set of numbers. If the number of elements in the list is even, we take the mean of the two middle numbers. Here are our current yield values:</p>
<p>$$4.2, 5.8, 3.9, 6.1, 4.7, 5.3$$</p>
<p>First, we sort the values:</p>
<p>$$3.9, 4.2, 4.7, 5.3, 5.8, 6.1$$</p>
<p>Since we have 6 values (even number), the median is the average of the two middle values:</p>
<p>$$\text{Median} = \frac{4.7 + 5.3}{2} = \frac{10}{2} = 5$$</p>
<p>In Python we can do this to calculate the median:</p>
<pre><code class="language-plaintext">import statistics

def calculate_median(values):
    return statistics.median(values)

# Example usage
data = [4.2, 5.8, 3.9, 6.1, 4.7, 5.3]
result = calculate_median(data)
print(f"Median: {result}")
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763102389405/52e5009b-6bc8-42c5-b8da-efe8c372fe96.png" alt="Python code in an image showing how to find the median" style="display:block;margin:0 auto" width="2080" height="1204" loading="lazy">

<h3 id="heading-variance-and-standard-deviation-measuring-spread">Variance and Standard Deviation: Measuring Spread</h3>
<p>Knowing the mean, mode, and median of data is helpful. But it’s also important to know how far away data points are from each other.</p>
<p>That’s where measures of <a href="https://en.wikipedia.org/wiki/Statistical_dispersion">dispersion</a> come in. Variance tells us, on average, how far numbers are from the mean.</p>
<p>Let’s see an example of how to calculate this.</p>
<p>Given yield data from the table:</p>
<p>$$4.2, 5.8, 3.9, 6.1, 4.7, 5.3$$</p>
<p>The first step is the calculate the mean:</p>
<p>$$\bar{x} = \frac{4.2 + 5.8 + 3.9 + 6.1 + 4.7 + 5.3}{6} = \frac{30}{6} = 5$$</p>
<p>The second step is to calculate the variance with the sample variance formula:</p>
<p>$$s^2 = \frac{\sum_{i=1}^{n}(x_i - \bar{x})^2}{n-1}$$</p>
<p>Let's apply the formula little by little to understand how it works.</p>
<p>We will first we will calculate the variance of each yield data point:</p>
<p>$$\begin{align*} (4.2 - 5.0)^2 &amp;= (-0.8)^2 = 0.64 \ (5.8 - 5.0)^2 &amp;= (0.8)^2 = 0.64 \ (3.9 - 5.0)^2 &amp;= (-1.1)^2 = 1.21 \ (6.1 - 5.0)^2 &amp;= (1.1)^2 = 1.21 \ (4.7 - 5.0)^2 &amp;= (-0.3)^2 = 0.09 \ (5.3 - 5.0)^2 &amp;= (0.3)^2 = 0.09 \end{align*}$$</p>
<p>Then we will sum all the squared differences:</p>
<p>$$\sum(x_i - \bar{x})^2 = 0.64 + 0.64 + 1.21 + 1.21 + 0.09 + 0.09 = 3.88$$</p>
<p>Now, we will finally find the variance:</p>
<p>$$s^2 = \frac{3.88}{6-1} = \frac{3.88}{5} = 0.776$$</p>
<p>The standard deviation is just the square root of the variance.</p>
<p>$$s = \sqrt{s^2} = \sqrt{0.776} \approx 0.881 tons/ha$$</p>
<p>Why is this useful?</p>
<p>It puts the spread back into the same units as the data, making it easier to interpret.</p>
<p>A small standard deviation means the data huddles close to the mean, while a large one means it’s widely scattered.</p>
<p>And here is a code example of how to calculate both:</p>
<pre><code class="language-plaintext">import statistics

def calculate_variance_and_std(values):
    variance = statistics.variance(values)
    std_dev = statistics.stdev(values)
    return variance, std_dev

# Example usage
data = [4.2, 5.8, 3.9, 6.1, 4.7, 5.3]
variance, std_dev = calculate_variance_and_std(data)
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_dev}")
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763102806607/a8236667-e4b0-48a5-9171-544c4b94096e.png" alt="Python code in an image showing how to find the variance and standard deviation" style="display:block;margin:0 auto" width="2148" height="1472" loading="lazy">

<h3 id="heading-what-is-the-normal-distribution-the-bell-curve-of-life">What Is the Normal Distribution? The Bell Curve of Life</h3>
<p>The normal distribution tells us how data naturally converges around the average value. Most values are focused on the center, and extreme values are more to the edges. This creates a bell curve.</p>
<p>By understanding this distribution, we can understand other distributions and also the central limit theorem.</p>
<p>To understand what normal distribution is, let’s look at it:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763529094535/f90ffdb8-543e-4d1f-9627-335e8f356512.png" alt="Image representing the normal distribution" style="display:block;margin:0 auto" width="582" height="426" loading="lazy">

<p>The normal distribution looks like like a mountain.</p>
<p>As you can see, most values are around the mean. Also, in and around the mean is the peak. Toward the extremes, the curve gets lower and lower. This means that in the extremes there are fewer and fewer values.</p>
<p>Normal distribution also has a formula associated with it:</p>
<p>$$f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left( -\frac{(x-\mu)^2}{2\sigma^2} \right)$$</p>
<p>I won’t go in depth into how the formula works here. I just want you to understand the main idea behind the concept.</p>
<p>There are many other distributions besides the normal distribution. Some of the most common are:</p>
<ul>
<li><p>Chi-squared distribution</p>
</li>
<li><p>Student’s t distribution</p>
</li>
<li><p>Bernoulli distribution</p>
</li>
<li><p>Binomial distribution</p>
</li>
<li><p>Poisson distribution</p>
</li>
</ul>
<p>Each distribution can model different events and phenomenons. For example the Chi-squared distribution is widely used to find the correlation between two phenomenons (sunburns and skin cancer, for example).</p>
<p>The Poisson distribution is also used in modeling counts of events, like the number of clients that enter a store per hour or the number of data packets that are transmitted in a Ethernet cable.</p>
<p>But it’s also possible to approximate a lot of distributions to the normal distribution using one of the most important theorems in all of mathematics: the central limit theorem. This is what we will explore next.</p>
<h3 id="heading-how-the-central-limit-theorem-helps-approximate-the-world">How the Central Limit Theorem Helps Approximate the World</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766902263857/9a03bb38-a7b9-4ef0-93f2-a7e0d80bd249.jpeg" alt="Person holding a small version of the world in their hand" style="display:block;margin:0 auto" width="5184" height="3456" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/person-holding-world-globe-facing-mountain-346885/">Porapak Apichodilok</a></p>
<p>The main idea of the central limit theorem is very simple:</p>
<ul>
<li>Most distributions can be approximated to become the normal distribution.</li>
</ul>
<p>This is just like pouring sand into a funnel. Grains may fall randomly, but over time the pile of sand will&nbsp;always begin to form the shape of a mountain.</p>
<p>This way, we can take many data points and average them. Over time, it will converge to become a normal distribution.</p>
<p>In other words, when independent random variables are all summed together, their sum tends toward a normal distribution.</p>
<p>Here is the formula:</p>
<p>$$\bar{X} \approx N\left(\mu, \frac{\sigma^2}{n}\right) \quad \text{or equivalently} \quad Z = \frac{\bar{X} - \mu}{\sigma/\sqrt{n}} \approx N(0, 1)$$</p>
<p>You don’t need to understand in depth what it means. Just understand that it’s a theorem that approximates other distributions to the normal distribution.</p>
<h4 id="heading-and-why-is-this-important">And why is this important?</h4>
<p>Because this theorem makes many billion-dollar industries possible.</p>
<p>Instead of testing every single possible scenario, we can test for a smaller amount of scenarios and assume that if it works for the smaller one, it will work for the bigger one.</p>
<p>For example, in telecommunications, instead of testing every possible phone call or data transmission, we can just test a few connections. If it works for those few connections, we can assume it will work for millions of phone and data transmissions.</p>
<p>For clinical trials, instead of testing a drug on millions of people, we can just test a smaller number of patients. If it works for a (relative) few patients, we can assume it will work on most people with the same condition.</p>
<p>Without this idea, clinical trials would not be possible. The same with telecommunications and so many other areas of engineering.</p>
<h3 id="heading-bayes-theorem-learning-from-evidence">Bayes Theorem: Learning from Evidence</h3>
<p>Now we’ll start looking at probability more in depth based on the data table we have been using.</p>
<p>Here’s the table again so that you can reference it more easily:</p>
<table>
<thead>
<tr>
<th>Farm</th>
<th>Yield (tons/ha)</th>
<th>Fertilizer Used (Kg/ha)</th>
<th>Rainfall (mm)</th>
</tr>
</thead>
<tbody><tr>
<td>A</td>
<td>4.2</td>
<td>150</td>
<td>280</td>
</tr>
<tr>
<td>B</td>
<td>5.8</td>
<td>220</td>
<td>420</td>
</tr>
<tr>
<td>C</td>
<td>3.9</td>
<td>120</td>
<td>230</td>
</tr>
<tr>
<td>D</td>
<td>6.1</td>
<td>250</td>
<td>480</td>
</tr>
<tr>
<td>E</td>
<td>4.7</td>
<td>200</td>
<td>340</td>
</tr>
<tr>
<td>F</td>
<td>5.3</td>
<td>200</td>
<td>390</td>
</tr>
</tbody></table>
<p>Now there are a lot of ideas and formulas related to probabilities. But here, I want to explain to you the core ones that are applied in AI and give you a high-level definition of things.</p>
<p>We’ll start with conditional probability, which is foundational to understanding Bayes’ theorem. Then we’ll get to the extended Bayes’ theorem formula.</p>
<p>So, let's get started!</p>
<h4 id="heading-what-is-conditional-probability">What is Conditional Probability?</h4>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766903189931/420cc60a-71cd-4c37-ab0a-f8aebe825ca7.jpeg" alt="Image of a person playing chess with the black pieces" style="display:block;margin:0 auto" width="6000" height="4000" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/black-and-yellow-chess-pieces-3830671/">KOUSHIK BALA</a></p>
<p>Conditional probability is the probability that an event will happen given that another event has already taken place.</p>
<p>Confused? Don't worry! Let's see an example:</p>
<p>Let’s say that:</p>
<ul>
<li><p>A = Farm has rainfall above or equal 400 mm</p>
</li>
<li><p>B = Farm has a yield above or equal to 5.0 tons/ha</p>
</li>
</ul>
<p>Here is the formula for Conditional Probability:</p>
<p>$$P(A|B) = \frac{P(A \cap B)}{P(B)}$$</p>
<p>Now let’s see this formula more in detail:</p>
<p>$$P(A)$$</p>
<p>This represents the probability that a farm has rainfall above or equal to 400 mm.</p>
<p>We have 6 farms, and 2 of them (farm B and D) have a rainfall above or equal to 400 mm.</p>
<p>So, the probability that a farm has rainfall above or equal to 400 mm is:</p>
<p>$$P(A) = \frac {2}{6} = \frac {1}{3} ≈ 0.33$$</p>
<p>Now let’s see for event B:</p>
<p>$$P(B)$$</p>
<p>This represents the probability that a farm has a yield above or equal to 5.0 tons/ha.</p>
<p>We have 6 farms and 3 of them (farm B, D and F) have a yield above or equal to 5.0 tons/ha.</p>
<p>So, the probability that a farm has a yield above or equal to 5.0 tons/ha is:</p>
<p>$$P(B) = \frac {3}{6} = \frac {1}{2} = 0.5$$</p>
<p>What about if we want to see both conditions’ probabilities at the same time?</p>
<p>$$P(A \cap B)$$</p>
<p>This refers to the probability of A and B being both true.</p>
<p>In our example, in means the probability that a farm both has a rainfall above or equal to 400 mm <strong>and</strong> a yield above or equal to 5.0 tons/ha.</p>
<p>We have:</p>
<ul>
<li><p>6 farms and 2 of them (farm B and D) have a rainfall above or equal 400 mm</p>
</li>
<li><p>6 farms and 3 of them (farm B, D and F) have a yield above or equal to 5.0 tons/ha</p>
</li>
</ul>
<p>For A and B to be true, only 2 farms (farm B and D) have both conditions.</p>
<p>This way:</p>
<p>$$P(A \cap B) = \frac {2}{6} = \frac {1}{3} ≈ 0.33$$</p>
<p>Now we’re ready to find out the conditional probability:</p>
<p>$$P(A|B)$$</p>
<p>This means the probability of A, knowing that B is true.</p>
<p>In our example, we can conclude that:</p>
<p>$$P(A|B) = \frac{P(A \cap B)}{P(B)} = \frac{0.33}{0.5} = 0.66$$</p>
<p>So, the probability that a farm has rainfall above or equal 400 mm – knowing that it has a yield above or equal to 5.0 tons/ha – is 0.66</p>
<h4 id="heading-bayes-theorem">Bayes’ Theorem</h4>
<p>This is one of the most important theorems in mathematics.</p>
<p>Bayes’ theorem is a formula that tells us how to change the probability of a prediction when new verified data becomes available.</p>
<p>In other words, it’s like a rule that tells us how to update our beliefs when new evidence appears.</p>
<p>Now, based on what we already know, let’s see how Bayes’ Theorem works.</p>
<p>Here is its formula:</p>
<p>$$P(B|A) = \frac{P(A|B) \cdot P(A)}{P(B)}$$</p>
<p>Now, based on the previous values, we can very easily find the probability of B, given that A is true.</p>
<p>In other words, the probability that a farm has a yield above or equal to 5.0 tons/ha given that is has a rainfall above or equal to 400 mm.</p>
<p>Let’s find the answer:</p>
<p>$$P(B|A) = \frac{P(A|B) \cdot P(A)}{P(B)}= \frac{0.66 \cdot 0.33}{0.5}=0.44$$</p>
<p>So, the probability that a farm has a a yield above or equal to to 5.0 tons/ha, knowing it rained equal to or more than 400 mm, is 44%.</p>
<p>Now that we’ve gone through this formula step by step, hopefully it doesn’t feel as complex.</p>
<h4 id="heading-where-is-this-applied-in-real-life">Where is this applied in real life?</h4>
<p>As with many math ideas in this book, Bayes' Theorem has applications in many business sectors.</p>
<p>For example, what is the best way to make a control system for a self-driving car, robot, or really any other device?</p>
<p>One effective approach is to use a <a href="https://en.wikipedia.org/wiki/Kalman_filter">Kalman filter</a>. Kalman filters rely heavily on Bayes' Theorem to handle control systems with incomplete data.</p>
<p>Kalman filters have a lot of applications in engineering. For example, thanks to Kalman filters, commercial jets can fly safely on autopilot.</p>
<p>So as you can see, Bayes’ Theorem is the foundation of many control systems used in risky industries.</p>
<h3 id="heading-what-are-markov-models-predicting-the-next-step-one-step-at-a-time">What Are Markov Models? Predicting the Next Step, One Step at a Time</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766902389612/c80d7118-f13d-4f9b-a149-861db3f2037d.jpeg" alt="Image of the hand of a person throwing dice into the air" style="display:block;margin:0 auto" width="6000" height="4000" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/person-about-to-catch-four-dices-1111597/">lil artsy</a></p>
<p>How do you predict the future with math? Markov chains allow you to do this to a certain degree.</p>
<p>For this reason, Markov chains are widely used in science, engineering, economics, and many other areas.</p>
<p>In addition to this, Markov decision processes are a very important foundation for reinforcement learning. Reinforcement learning is a branch of AI where agents learn to make decisions by interacting with an environment to maximize rewards.</p>
<p>In this section, I’ll introduce you to Markov chains and decision processes with an analogy, a plain English explanation, and a code example.</p>
<p>If you want to dive in further, I recommend my <a href="https://www.freecodecamp.org/news/what-is-a-markov-chain/">freeCodeCamp article on the subject</a>.</p>
<h4 id="heading-markov-chain-analogy">Markov Chain Analogy</h4>
<p>Imagine that you want to predict the weather tomorrow, and it <strong>only</strong> depends on the weather today. The weather can be either sunny or rainy.</p>
<p>Here are the probabilities:</p>
<ul>
<li><p>If it's sunny today, there's an 80% chance that it will be sunny again tomorrow, and a 20% chance that it will be rainy.</p>
</li>
<li><p>If it's rainy today, there's a 50% chance that it will be sunny tomorrow, and a 50% chance that it will be rainy.</p>
</li>
</ul>
<p>In this scenario, we can predict future states of the weather based on current states using probabilities.</p>
<p>This idea of predicting the future based solely on probabilities of the present is called a Markov chain.</p>
<p>Here, the states are either sunny or rainy and the probabilities describe the chances of the weather changing based on the current state.</p>
<h4 id="heading-markov-chain-explained-in-plain-english">Markov Chain Explained in Plain English</h4>
<p>A Markov chain describes random processes where systems move between states, and a new state only depends on the current state, not on how it got there.</p>
<p>Mathematically, Markov chains are called stochastic models because they model (simulate) real life events that are random by nature (stochastic).</p>
<p>Markov chains are popular because they are easy to implement and efficient at modeling complex systems.</p>
<p>Another key advantage is their "memoryless" property. This makes it faster to run on computers, and powerful to study random processes and make predictions based on current conditions.</p>
<h4 id="heading-applications-of-markov-chains">Applications of Markov Chains</h4>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766902558494/8129d378-5cd8-4fdc-be48-8ba0a34181b7.jpeg" alt="Image of a white square with a dark star inside it, surrounded by many other dark squares" style="display:block;margin:0 auto" width="3840" height="2160" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/shapes-on-a-dark-background-25630338/">Google DeepMind</a></p>
<p>At some level, almost all real-life events are stochastic. In other words, they involve randomness and uncertainty.</p>
<p>This is exactly why they are so widely used.</p>
<p>They can predict the behavior of systems based on current conditions:</p>
<ul>
<li><p>In finance, they are used to detect changes in credit ratings for forecasting market regimes.</p>
</li>
<li><p>In genetics, they help understand how proteins change over time (which is important when studying genetic variations).</p>
</li>
</ul>
<p>These real life examples show how effective Markov chains can be used to solve real problems in different fields.</p>
<p>In AI, Markov chains are used to model an environment like a factory or home. Modeling an environment with Markov chains is called a Markov decision process.</p>
<p>Using a Markov decision process, it’s possible to use reinforcement learning to create and optimize agents to act in the environment.</p>
<p>Of course, new and better variants of the Markov decision process have appeared over the years. But the key idea here is that it is thanks to Markov decision processes that the basis for reinforcement learning exists.</p>
<p>Reinforcement learning is widely used in advertising systems, logistics, robotics, video games, and many more applications.</p>
<h4 id="heading-types-of-markov-chains">Types of Markov Chains</h4>
<p>There are many types of Markov chains. In this section, we'll only discuss the most important variants.</p>
<ol>
<li>Discrete-Time Markov Chains (DTMCs)</li>
</ol>
<p>In DTMCs, the system changes state at specific time steps. They are called discrete because the state transitions occur at distinct, separate time intervals.</p>
<p>They are used in queuing theory (study of the behavior of waiting lines), genetics, and economics because they are simple to analyze.</p>
<ol>
<li>Continuous-Time Markov Chains (CTMCs)</li>
</ol>
<p>CTMCs differ from DTMCs in that state transitions can occur at any continuous time point, not at fixed intervals.</p>
<p>This makes them stochastic models where state changes happen continuously. This is important in chemical reactions and reliability engineering.</p>
<ol>
<li>Reversible Markov Chains</li>
</ol>
<p>Reversible Markov chains are special. The process of state change is the same whether the direction is forwards or backwards, like rewinding a video and playing it again.</p>
<p>This property makes it easier to know when a system is stable and study how a system behaves over time. They are widely used in statistical physics and economics</p>
<ol>
<li>Doubly Stochastic Markov Chains</li>
</ol>
<p>Doubly stochastic Markov chains are defined by a transition probability matrix. In the matrix, the sum of the probabilities in each row and each column equals 1.</p>
<p>This means each row and each column represent a valid probability distribution. In other words, each row and column represent a list of chances for different outcomes.</p>
<p>This property is crucial in quantum computing and statistical mechanics.</p>
<p>Thanks to Doubly stochastic Markov chains, systems change in a way that preserves probabilities and symmetry, making the modeling and analysis of quantum computing systems far more accurate.</p>
<h4 id="heading-hidden-markov-chains-code-example">Hidden Markov Chains Code Example</h4>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766903059652/ad8c6509-87ae-4978-8b64-24146161d1cb.jpeg" alt="Image of glasses, a MAC computer, and blurry code in it" style="display:block;margin:0 auto" width="3353" height="2514" loading="lazy">

<p>Photo by <a href="https://www.pexels.com/photo/data-codes-through-eyeglasses-577585/">Kevin Ku</a></p>
<p>Before we jump into code examples, let’s first understand what Hidden Markov Chains are.</p>
<p>The main idea behind hidden Markov chains is to model systems that have hidden states (states for which we don’t know their values) which can only be discovered through observable events.</p>
<p>In other words, hidden Markov chains allow us to predict the behavior of a system by:</p>
<ul>
<li><p>Considering the likelihood of moving from one state to another.</p>
</li>
<li><p>Knowing the probability of observing a certain event from each state</p>
</li>
</ul>
<p>We can understand this by observing how the states change from an indirect point of view.</p>
<p>We may not know the states’ original values. But by knowing the way they change, we can predict what their values will be in the future.</p>
<p>This way, hidden Markov chains are flexible in modeling sequences, capturing both the transitions between hidden states and the observable outcomes.</p>
<p>Because of this, hidden Markov models are used in fields such as engineering, financial modeling, speech recognition, bioinformatics, and many more.</p>
<h4 id="heading-code-example">Code Example:</h4>
<p>In this code example, we’ll see a simple example with synthetic data.</p>
<p>Here is the full code:</p>
<pre><code class="language-python">import numpy as np
from hmmlearn import hmm

# Set random seed for reproducibility
np.random.seed(42)

# Define the HMM parameters
n_components = 2  # Number of states
n_features = 1    # Number of observation features

# Create a Gaussian HMM
model = hmm.GaussianHMM(n_components=n_components, covariance_type="diag")

# Define transition matrix (rows must sum to 1)
model.startprob_ = np.array([0.6, 0.4])
model.transmat_ = np.array([[0.7, 0.3],
                            [0.4, 0.6]])

# Define means and covariances for each state
model.means_ = np.array([[0.0], [3.0]])
model.covars_ = np.array([[0.5], [0.5]])

# Generate synthetic observation data
X, Z = model.sample(100)  # 100 samples

# Create a new HMM instance
new_model = hmm.GaussianHMM(n_components=n_components, covariance_type="diag", n_iter=100)

# Fit the model to the data
new_model.fit(X)

# Print the learned parameters
print("Transition matrix:")
print(new_model.transmat_)
print("Means:")
print(new_model.means_)
print("Covariances:")
print(new_model.covars_)

# Predict the hidden states for the observed data
hidden_states = new_model.predict(X)

print("Hidden states:")
print(hidden_states)
</code></pre>
<img src="https://cdn-media-0.freecodecamp.org/2024/06/1.png" alt="Full code example of HMM (Hidden Markov Chain)" style="display:block;margin:0 auto" width="2000" height="2528" loading="lazy">

<p>Now let’s break the code down block by block:</p>
<p><strong>Import libraries and set random seed:</strong></p>
<pre><code class="language-python">import numpy as np
from hmmlearn import hmm

np.random.seed(42)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763529887680/2440547e-ccf4-4067-83c2-20fafb16f045.png" alt="Code example of HMM (Hidden Markov Chain) - Import libraries and set random seed" style="display:block;margin:0 auto" width="2080" height="772" loading="lazy">

<p>In this block of code, we imported two Python libraries:</p>
<ul>
<li><p><a href="https://numpy.org/">NumPy</a>: For numerical operations.</p>
</li>
<li><p><a href="https://hmmlearn.readthedocs.io/en/latest/index.html">hmmlearn</a>: For hidden Markov model implementation.</p>
</li>
</ul>
<p>Next we defined a random seed with the NumPy library. A random seed is a value used to start a pseudorandom number generator.</p>
<p>With a fixed random seed, we can ensure that the sequence of pseudorandom numbers generated is always the same. This allows us to duplicate experiments and verify results.</p>
<p>The specific value of the seed doesn’t matter as long as it remains consistent.</p>
<p><strong>Define the HMM parameters and create a Gaussian HMM:</strong></p>
<pre><code class="language-python">n_components = 2  # Number of states
n_features = 1    # Number of observation features

model = hmm.GaussianHMM(n_components=n_components, covariance_type="diag")
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763529894398/094ac272-2788-4856-a984-b1f687464e90.png" alt="Code example of HMM (Hidden Markov Chain) - Define the HMM parameters and create a Gaussian HMM" style="display:block;margin:0 auto" width="2988" height="772" loading="lazy">

<p>In this code block, we created an HMM with two hidden states and a single observed variable.</p>
<p><code>covariance_type "diag"</code> means the matrices that represent covariance (how two variables change together) are diagonal. In other words, each row and column is assumed to be independent of the others.</p>
<p>This implies that the probability distributions of each row and column are independent of each other.</p>
<p>But there is still something strange when we defined the hidden Markov chain:</p>
<p><strong>What does “Gaussian“ mean?</strong></p>
<p>This is a very big topic in statistics, but in a few words, Markov chains can only be created when we specify the transition probabilities (chances of moving from one state to another in a Markov chain) and an initial probability distribution.</p>
<p>A Gaussian HMM assumes events are initially modeled by a Gaussian distribution, also called a normal distribution!</p>
<p>And recall, we have already seen before what a normal distribution is.</p>
<p>Here is it again:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763529107399/e51cb7a3-e751-45c7-8164-c07795ad32e1.png" alt="Code example of HMM (Hidden Markov Chain) - Image of normal distribution" style="display:block;margin:0 auto" width="582" height="426" loading="lazy">

<p>From a normal distribution and other components, we can create a hidden Markov chain. And hidden Markov chains serve as a foundation for systems that affect millions of lives.</p>
<p><strong>Define transition matrix, means, and covariances for each state:</strong></p>
<pre><code class="language-python">model.startprob_ = np.array([0.6, 0.4])
model.transmat_ = np.array([[0.7, 0.3],
                            [0.4, 0.6]])

model.means_ = np.array([[0.0], [3.0]])
model.covars_ = np.array([[0.5], [0.5]])
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763529901607/53442504-bcec-46d0-8114-fcd627947576.png" alt="Code example of HMM (Hidden Markov Chain) - Define transition matrix, means, and covariances for each state" style="display:block;margin:0 auto" width="2080" height="952" loading="lazy">

<pre><code class="language-python">model.startprob_ = np.array([0.6, 0.4])
</code></pre>
<p>This line sets the initial state probabilities for a Hidden Markov Model (HMM). It points out that there is a 60% probability of starting in state 0 and a 40% probability of starting in state 1.</p>
<pre><code class="language-python">model.transmat_ = np.array([[0.7, 0.3], [0.4, 0.6]])
</code></pre>
<p>This line of code sets the state transition probability matrix for the HMM.</p>
<p>The matrix specifies the probabilities of moving from one state to another:</p>
<ul>
<li><p>From state 0, there is a 70% chance of staying in state 0 and a 30% chance of transitioning to state 1.</p>
</li>
<li><p>From state 1, there is a 40% chance of transitioning to state 0 and a 60% chance of staying in state 1.</p>
</li>
</ul>
<pre><code class="language-python">model.means_ = np.array([[0.0], [3.0]])
</code></pre>
<p>This line sets the mean values for the observation distributions in each state.</p>
<p>It indicates that the observations are normally distributed with a mean of 0.0 in state 0 and a mean of 3.0 in state 1.</p>
<pre><code class="language-python">model.covars_ = np.array([[0.5], [0.5]])
</code></pre>
<p>This line sets the covariance values for the observation distributions in each state.</p>
<p>It specifies that the variance (covariance in this 1-dimensional case) of the observations is 0.5 for both state 0 and state 1.</p>
<p><strong>Create data, new HMM instance, and fit the model with the data:</strong></p>
<pre><code class="language-python">X, Z = model.sample(100)  # 100 samples

new_model = hmm.GaussianHMM(n_components=n_components, covariance_type="diag", n_iter=100)

new_model.fit(X)

print("Transition matrix:")
print(new_model.transmat_)
print("Means:")
print(new_model.means_)
print("Covariances:")
print(new_model.covars_)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763529906427/009804bc-40db-4979-99dd-564935b175cc.png" alt="Code example of HMM (Hidden Markov Chain) - Create data, new HMM instance, and fit the model with the data" style="display:block;margin:0 auto" width="2000" height="845" loading="lazy">

<p>In this code, we created a model with 100 samples, iterated it 100 times, and printed the new state transition matrix, means, and covariances.</p>
<p>In other words, we:</p>
<ol>
<li><p>Generated 100 samples from the original model</p>
</li>
<li><p>Fitted a new HMM to these samples.</p>
</li>
<li><p>Printed the learned parameters of this new model.</p>
</li>
</ol>
<p>What do X and Z mean here?</p>
<p>X means the observed data samples generated by the original model, while Z means the hidden state sequences corresponding to the observed data samples generated by the original model.</p>
<p>The transition matrix prints out:</p>
<pre><code class="language-python">[[0.8100804  0.1899196 ]
 [0.49398918 0.50601082]]
</code></pre>
<p>Which means that the model tends to stay in state 0 and has nearly equal chances of switching or staying when in state 1.</p>
<p>The means print out:</p>
<pre><code class="language-python">[[0.01577373]
 [3.06245496]]
</code></pre>
<p>Which means that the average observed value is approximately 0.016 in state 0 and 3.062 in state 1.</p>
<p>The covariances print out:</p>
<pre><code class="language-python">[[[0.41987084]]
 [[0.53146802]]]
</code></pre>
<p>Which means that the observed values vary by about 0.420 in state 0 and 0.531 in state 1.</p>
<p>This way, we may never know the exact values of the states, but we know their average observed value and how they vary and tend to change with each other.</p>
<p><strong>Predict the hidden states for the observed data:</strong></p>
<pre><code class="language-python">hidden_states = new_model.predict(X)

print("Hidden states:")
print(hidden_states)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1763529913530/f81b3dbf-f517-4857-ac92-4732a524a621.png" alt="Code example of HMM (Hidden Markov Chain) - Predict the hidden states for the observed data" style="display:block;margin:0 auto" width="2080" height="772" loading="lazy">

<p>In this code, based on the X observed data samples, we predicted the new states of the Markov model.</p>
<p>The hidden states print out:</p>
<pre><code class="language-python">[0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 1 1 0 1 1 0 1 0 0 0 1
 1 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0
 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0]
</code></pre>
<p>Which means that the hidden states switch between state 0 and state 1, showing how the system changes states over time.</p>
<h3 id="heading-applications-in-ai-and-control-theory-making-decisions-under-uncertainty"><strong>Applications in AI and Control Theory: Making Decisions Under Uncertainty</strong></h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765002495967/325e5ee4-df14-4adc-a520-0764d89fe8c8.jpeg" alt="Image of many flight instruments in an airplane" style="display:block;margin:0 auto" width="5074" height="3325" loading="lazy">

<p><a href="https://www.pexels.com/photo/gray-airplane-control-panel-3402846/">Photo by capt.sopon</a></p>
<p>I have been giving you a high-level overview of the field of probabilities and statistics. As I explained before, I wanted to make the explanations simple to understand.</p>
<p>As someone with a bachelor's degree in electrical and computer engineering, I can assure you that while this chapter seems simple, in probabilities and statistics, things can get very complicated very quickly.</p>
<p>Many more concepts like:</p>
<ul>
<li><p>p-values</p>
</li>
<li><p>Advanced Monte Carlo methods</p>
</li>
<li><p>Bayesian networks</p>
</li>
<li><p>Statistical hypotheses</p>
</li>
</ul>
<p>Are not as straightforward as the ideas I’ve just told you about.</p>
<p>But as it is, probability and statistics are the starting points for making decisions where uncertainty exists in AI and control theory.</p>
<p>For example, the Bayes’ theorem, besides being the foundation of the Kalman filter, is also the foundation of many probabilistic models in the field of AI. Probabilistic models are usually used in quant firms and banks to model risk.</p>
<p>In control theory, probabilities and statistics are widely used to design robust control systems (as is the case with Kalman filters).</p>
<p>So as you can see, the application of probabilities and statistics, as with calculus and linear algebra, is the foundation for many tools that impact millions of lives and move billions of dollars in the global economy.</p>
<h2 id="heading-chapter-7-optimization-theory-teaching-machines-to-improve">Chapter 7: Optimization Theory - Teaching Machines to Improve</h2>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765002637327/9dea740c-4582-42bf-95a6-1230b7e9092d.jpeg" alt="Black and white image of many railways originating from a single one" style="display:block;margin:0 auto" width="2560" height="1920" loading="lazy">

<p><a href="https://www.pexels.com/photo/railroad-tracks-in-city-258510/">Photo by Pixabay</a></p>
<p>This is the most advanced math chapter of the book. To truly understand it, it’s very important that you’ve first read the other chapters first.</p>
<p>We’re going to examine a few machine learning methods, and I’ll show you some recipes of how machine learning is just the use of linear algebra, calculus, probabilities and statistics, and optimization theory.</p>
<p>Just like making a cake!</p>
<h3 id="heading-what-is-optimization-theory">What is Optimization Theory?</h3>
<p>In AI, optimization theory is responsible for the algorithms that optimize data-driven AI models.</p>
<p>Often, big companies invest millions in research to create or refine algorithms that make training AI models faster.</p>
<p>This way, companies save far more money than the upfront research costs when scaling to train multiple large AI models.</p>
<p>It is thanks to optimization theory that deep learning was able to scale efficiently, eventually leading to the creation of ChatGPT and many other large language models.</p>
<p><strong>But why is that?</strong></p>
<p>In all data-driven machine learning models, there is a learning phase that has to happen. That is, there’s a period where the algorithms make predictions that are not correct and then need to change some parameters to make sure the next predictions are correct – or at least closer to being correct.</p>
<p>Without optimization, machine learning algorithms don't get anywhere on their learning path to the right solution. Without optimization, they spend too much time on a learning path that won’t increase their ability to predict things the right way.</p>
<p>So, let’s start learning!</p>
<h3 id="heading-why-optimization-drives-learning-in-ai">Why Optimization Drives Learning in AI</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766903297889/4075d065-9b55-42e2-a6f6-8aae02de940f.jpeg" alt="Image of a very cute white robot" style="display:block;margin:0 auto" width="4896" height="3264" loading="lazy">

<p><a href="https://www.pexels.com/photo/high-angle-photo-of-robot-2599244/">Photo by Alex Knight</a></p>
<p>Optimization theory is the mathematical foundation that allows algorithms to improve their performance over many iterations.</p>
<p>When we combine an algorithm with a path to change its parameters to meet a certain objective (done with an optimization method), it’s called a machine learning algorithm.</p>
<p>This learning process always involves minimizing or maximizing a certain objective. For example, for many machine learning algorithms, the main objective is to minimize errors. To do this, over many iterations, the optimization methods "tells" the internal components of an algorithm what to change after receiving feedback on how well it’s performing.</p>
<p>It’s like someone first learning how to drive a car. The first few times, it may be complicated. But after a while and some practice, the driver learns how to drive properly and not make the same mistakes they once did in the past with the help of the instructor.</p>
<p>The same applies to optimization methods when optimizing algorithms.</p>
<h4 id="heading-types-of-optimization-theory-methods-in-ml-and-deep-learning">Types of Optimization Theory Methods in ML and Deep Learning</h4>
<p>The field of optimization theory is huge! Just as with many fields of mathematics, it is constantly growing every year.</p>
<p>But for the purposes of this book, there are three main categories of optimization methods:</p>
<ol>
<li><strong>First-Order Methods</strong></li>
</ol>
<p>These are the most used in deep learning and in all LLM models like Gemini, Grok, and others.</p>
<p>They are called first-order methods because they all use the first derivative of functions. The first derivative of a function measures how much a function's output changes when its input changes very little. The most widely used in deep learning are advanced variants of gradient descent.</p>
<p>While there are many variants, here are some popular examples:</p>
<ul>
<li><p>Standard batch gradient descent</p>
</li>
<li><p>Stochastic gradient descent</p>
</li>
<li><p>Mini-batch gradient descent</p>
</li>
<li><p>RMSprop</p>
</li>
<li><p><strong>Adam</strong></p>
</li>
</ul>
<p>In this chapter, we will look in depth at one of these methods called <strong>Adam</strong> (below).</p>
<ol>
<li><strong>Second-Order Methods</strong></li>
</ol>
<p>They are called second-order methods because they use information from second derivatives for better updates. There are many methods, like:</p>
<ul>
<li><p>BFGS</p>
</li>
<li><p>L-BFGS</p>
</li>
<li><p>Newton's method</p>
</li>
</ul>
<p>But these are not often used in machine and deep learning. While they optimize with fewer iterations, for the type of optimization problems algorithms in AI create (high-dimensional problems), they’re very computationally expensive.</p>
<p>So they’re not widely used like first-order optimization methods.</p>
<ol>
<li><strong>Zeroth-Order and Other Methods</strong></li>
</ol>
<p>These methods do not require derivatives to optimize algorithms. Some examples of algorithms where derivatives are not used are:</p>
<ul>
<li><p>Genetic algorithms</p>
</li>
<li><p>Dynamic programming algorithms</p>
</li>
<li><p>Particle swarm optimization methods</p>
</li>
</ul>
<p>The problem with these algorithms is that they are often very slow for many variables.</p>
<p>But in certain AI contexts, they can help optimize the architecture of deep learning models to improve AI models from an architectural point of view (instead of a parameter point of view).</p>
<h4 id="heading-how-does-optimization-theory-connect-with-linear-algebra-calculus-and-probability-and-statistics">How does optimization theory connect with linear algebra, calculus, and probability and statistics?</h4>
<p>Essentially:</p>
<ul>
<li><p>Calculus teaches you derivatives, which help you understand optimization theory.</p>
</li>
<li><p>Linear algebra teaches you matrices, which help you understand how different states relate and transform.</p>
</li>
<li><p>Probability and statistics teach you concepts like covariance and correlation, which help you understand how variables are connected with each other.</p>
</li>
</ul>
<p>This way, with linear algebra and probability and statistics, you gain the knowledge necessary to understand the algorithms. With calculus you gain the basis to understand optimization theory and how it changes certain parameters of the fundamental algorithms to minimize/maximize a certain objective.</p>
<h3 id="heading-simple-optimization-techniques-how-machines-learn-step-by-step">Simple Optimization Techniques: How Machines Learn Step by Step</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765002727335/a265939c-dea8-4763-8861-7c7a0dbe1081.jpeg" alt="Image of a Star Wars blue and white robot" style="display:block;margin:0 auto" width="4608" height="3072" loading="lazy">

<p><a href="https://www.pexels.com/photo/star-wars-r2-d2-2085831/">Photo by LJ Checo</a></p>
<p>Now, we’re going to see examples of machine learning algorithms used for optimization and deconstruct them so that you can understand how these areas of mathematics apply to them.</p>
<p>In each example, I will explain their main idea with an analogy as well as how each math area is used in each algorithm.</p>
<h4 id="heading-linear-regression">Linear Regression</h4>
<p>Imagine that you are solving a puzzle. To complete the puzzle, you need to arrange the pieces in the right design/order.</p>
<p>The same idea applies to linear regression.</p>
<p>We have matrices (linear algebra) that represent the parameters of the linear regression model and the data that flow into it.</p>
<p>And we can see over time how well the line is fitting the numbers, as well as its error (probabilities and statistics).</p>
<p>To find the best line for the linear regression, we need to know how much the parameters of the model need to change (calculus) and actually apply that change to the parameters (optimization theory).</p>
<p>This way, calculus tells us which direction to change the parameters, and optimization theory tells us how much to actually change them.</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764295886800/0c5efd95-9368-4b68-b945-ff911632ca4c.gif" alt="GIF animation of linear regression working over many iterations" style="display:block;margin:0 auto" width="1037" height="856" loading="lazy">

<p>Let’s see how to code the linear regression above:</p>
<pre><code class="language-python">import numpy as np

np.random.seed(42)
X = np.linspace(0, 10, 50)
y_true = 3 * X + 2
noise = np.random.normal(0, 2, 50)
y = y_true + noise

w = 0.1 
b = 0.5
learning_rate = 0.01
iterations = [0, 1, 2, 3, 4, 5]
saved_states = []

for epoch in range(max(iterations) + 1):
    y_pred = w * X + b
    error = np.mean((y - y_pred) ** 2)
    
    if epoch in iterations:
        saved_states.append({
            'epoch': epoch,
            'w': w,
            'b': b,
            'y_pred': y_pred.copy(),
            'error': error
        })
    
    dw = -2 * np.mean(X * (y - y_pred))
    db = -2 * np.mean(y - y_pred)
    
    w = w - learning_rate * dw
    b = b - learning_rate * db
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765335029715/f77be0d9-ea3d-48f1-8cb5-f4806d1295e6.png" alt="Linear regression code example - full code example" style="display:block;margin:0 auto" width="2080" height="3272" loading="lazy">

<p>Let’s see the code block by block:</p>
<p><strong>Import library:</strong></p>
<pre><code class="language-plaintext">import numpy as np
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765335026504/94989760-bb16-4469-947e-eba7bd25b5be.png" alt="Linear regression code example - Import library" style="display:block;margin:0 auto" width="2080" height="528" loading="lazy">

<p>For this problem, we’ll import one of the most used Python libraries: NumPy (which we’ve worked with earlier in the book).</p>
<p><strong>Create data points:</strong></p>
<pre><code class="language-python">np.random.seed(42)
X = np.linspace(0, 10, 50)
y_true = 3 * X + 2
noise = np.random.normal(0, 2, 50)
y = y_true + noise
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765335038511/59e01c3d-27bf-4e6c-8500-9178f1ff569f.png" alt="Linear regression code example - Create data points" style="display:block;margin:0 auto" width="2080" height="844" loading="lazy">

<p>In this code, we define a base line that will help in generating the data points:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765336338665/caa859d0-92cb-424e-8eb2-292093c24355.png" alt="Linear regression code example - image of green base line that will help in generating the data points" style="display:block;margin:0 auto" width="753" height="565" loading="lazy">

<pre><code class="language-python">X = np.linspace(0, 10, 50)
y_true = 3 * X + 2
</code></pre>
<p>After this green line has been created, we will add noise to it to create the data points:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765336395290/80849617-9489-471d-88f6-fb2aaea5b385.png" alt="Linear regression code example - image of a green baseline that will help in generating the data points with blue dots added by introduced noise" style="display:block;margin:0 auto" width="756" height="580" loading="lazy">

<pre><code class="language-plaintext">noise = np.random.normal(0, 2, 50)
y = y_true + noise
</code></pre>
<p>This is how we defined the data points for the line dataset.</p>
<p><strong>Initializing linear regression parameters and others:</strong></p>
<pre><code class="language-python">w = 0.1 
b = 0.5
learning_rate = 0.01
iterations = [0, 1, 2, 3, 4, 5]
saved_states = []
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765335044810/72a775ee-9929-488d-b05e-ab5d32d6b031.png" alt="Linear regression code example - Initializing linear regression parameters and others" style="display:block;margin:0 auto" width="2080" height="844" loading="lazy">

<p>In this block of code, we initialize:</p>
<ul>
<li><p>Linear regression parameters: Weight to be 0.1 and bias to be 0.5</p>
</li>
<li><p>One hyperparameter: Learning rate</p>
</li>
<li><p>How many iterations we are going to use to improve the linear regression</p>
</li>
<li><p>An array called saved_states to store values to later create graphs</p>
</li>
</ul>
<p>This way, we start with this red line:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765336283612/d7bb34b5-aefc-4565-bed2-d2819bc449df.png" alt="Linear regression code example - initializing linear regression parameters and line to fit data points starting with near zero slope" style="display:block;margin:0 auto" width="735" height="575" loading="lazy">

<p><strong>Making the linear regression learn with the data:</strong></p>
<pre><code class="language-python">for epoch in range(max(iterations) + 1):
    y_pred = w * X + b
    error = np.mean((y - y_pred) ** 2)
    
    if epoch in iterations:
        saved_states.append({
            'epoch': epoch,
            'w': w,
            'b': b,
            'y_pred': y_pred.copy(),
            'error': error
        })
    
    dw = -2 * np.mean(X * (y - y_pred))
    db = -2 * np.mean(y - y_pred)
    
    w = w - learning_rate * dw
    b = b - learning_rate * db
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765335055978/2395671a-d873-4bd1-bfa0-349cc6c7be65.png" alt="Linear regression code example - Making the linear regression learn with the data" style="display:block;margin:0 auto" width="2080" height="2012" loading="lazy">

<p>It may appear complicated, but let’s see in smaller blocks:</p>
<ul>
<li>For loop</li>
</ul>
<pre><code class="language-python">for epoch in range(max(iterations) + 1):
</code></pre>
<ul>
<li>Making an prediction and seeing its error</li>
</ul>
<pre><code class="language-python">y_pred = w * X + b
error = np.mean((y - y_pred) ** 2)
</code></pre>
<p>In this block of the code, we find the values predicted for the current parameters and see its error from the real values.</p>
<ul>
<li>Saving current iteration values for future statistics</li>
</ul>
<pre><code class="language-plaintext">if epoch in iterations:
     saved_states.append({
         'epoch': epoch,
         'w': w,
         'b': b,
         'y_pred': y_pred.copy(),
         'error': error
     })
</code></pre>
<p>Here we are juts storing in the saved_states array the values of the current iteration to later compute images.</p>
<ul>
<li>Finding the gradients</li>
</ul>
<pre><code class="language-plaintext">dw = -2 * np.mean(X * (y - y_pred))
db = -2 * np.mean(y - y_pred)
</code></pre>
<p>In this block of code, we find the gradients values for the current prediction.</p>
<p>In other words, for the weight and bias, we find out how much they need to change in order to approximate better the values of the parameters to the data points.</p>
<ul>
<li>Updating the parameters values</li>
</ul>
<pre><code class="language-plaintext">w = w - learning_rate * dw
b = b - learning_rate * db
</code></pre>
<p>Finally, we update the weight and the bias with the new values so that the line better approximates the data points:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765335279159/97e4914a-ed8a-4cf7-8155-e7cde0fa7edd.gif" alt="GIF animation of linear regression working over many iterations" style="display:block;margin:0 auto" width="1037" height="856" loading="lazy">

<h4 id="heading-neural-networks">Neural Networks</h4>
<p>The same puzzle idea applies to neural networks. Neural networks are algorithmic models inspired by the brain that learn patterns from data. They are part of a machine learning field called deep learning, which uses neural networks to learn complex patterns.</p>
<p>Neural networks are important because they power modern AI applications like:</p>
<ul>
<li><p>Image recognition</p>
</li>
<li><p>Language translation</p>
</li>
<li><p>Chatbots</p>
</li>
</ul>
<p>For example, ChatGPT means Chat Generative Pre-trained Transformer. A transformer is an architecture of neural networks.</p>
<p>If you understand neural networks, you’ll understand the foundations that make ChatGPT work.</p>
<ul>
<li><p>We have matrices (linear algebra) that represent the parameters of the neural network model and the data that flow into it.</p>
</li>
<li><p>And we can know over time how well the neural network model is converging to the dataset, fitting the numbers, and see its error (probabilities and statistics).</p>
</li>
<li><p>Calculus will tell us in which direction the parameters of the neural network need to change.</p>
</li>
<li><p>Optimization theory will tell us how much they need to change.</p>
</li>
</ul>
<p>For example, this is a neural network:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1764296443948/e1f46e04-d508-407c-8da6-de8e267a2ba7.png" alt="Image example of a simple neural network" style="display:block;margin:0 auto" width="655" height="391" loading="lazy">

<p>This model has in total 13 parameters:</p>
<ul>
<li><p>It has 10 lines(connections between circles). These are called weights.</p>
</li>
<li><p>It has 2 circles in the hidden layer and 1 in the output layer. Each circle has one bias.</p>
</li>
</ul>
<p><strong>Big question:</strong></p>
<p>Imagine you work in a bank. You are in charge of deciding who gets credit cards or not. For that, you create the neural network above that takes 4 inputs:</p>
<ul>
<li><p>Income</p>
</li>
<li><p>Credit score</p>
</li>
<li><p>Debt ratio</p>
</li>
<li><p>Bankruptcy history</p>
</li>
</ul>
<p>With this neural network well optimized, you can figure it out!</p>
<p>Very simply, without going into things like activation functions, the network processes the 4 inputs through its weights and biases.</p>
<p>Each connection multiplies the input by its weight. After that, each node adds its bias.</p>
<p>The final output is a number between 0 and 1:</p>
<ul>
<li><p>Numbers close to 0 mean "Not approved"</p>
</li>
<li><p>Numbers close to 1 mean "Approved"</p>
</li>
</ul>
<p>For example, a high income figure, a good credit score, and no bankruptcy history data flow through the neural networks and produce 0.92. This means that it should be approved.</p>
<p>But a low income figure with a history of bankruptcy may produce 0.15, which results in a not approved.</p>
<p>In reality, bank systems and others have neural networks that take far more well-chosen parameters and decide this automatically.</p>
<p>This is precisely how AI can be used for credit approval.</p>
<p>But a question remains: What is the best way to know how much the parameters need to change?</p>
<p>In the next part, we are going to see the most famous optimization theory algorithm that will help us decide that.</p>
<h3 id="heading-what-is-adam-the-most-popular-way-ai-models-finds-the-best-learning-path">What is Adam? The Most Popular Way AI Models Finds the Best Learning Path</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766902926221/0b6fbbee-dfda-4a55-bd5d-21215ea33074.jpeg" alt="Image of a mountain" style="display:block;margin:0 auto" width="6000" height="4000" loading="lazy">

<p><a href="https://www.pexels.com/photo/green-leafed-trees-during-fog-time-167684/">Photo by Lum3n</a></p>
<p>To optimize neural network based AI models, one of the most popular methods is called Adam, which means Adaptive Moment Estimation.</p>
<p>The paper that introduced the method is one of the most influential in the 21st century in machine learning, with thousands of citations. As with all ideas in non-symbolic AI, Adam is a mixture of different math concepts.</p>
<p>It's composed of the ideas of two other optimization methods:</p>
<ul>
<li><p>Momentum Gradient Descent: Accumulates velocity from previous gradients to move faster in consistent directions</p>
</li>
<li><p>Root Mean Square Propagation (RMSProp): Adapts learning rates based on recent gradient magnitudes</p>
</li>
</ul>
<p><strong>Let's understand them with an analogy.</strong></p>
<p>Imagine that you are riding a bicycle down a mountain little by little. You already know the direction thanks to calculus.</p>
<p>But how do you descend safely without losing control or going too slowly?</p>
<p>First, you need to build up speed gradually using past momentum. This is one of the main ideas of momentum gradient descent.</p>
<p>It's also important that you adjust your speed based on the terrain's elevation. This is the main idea of RMSProp.</p>
<p>This way, you can safely accelerate and brake appropriately.</p>
<p>When optimizing a model with Adam, this is the same concept. With Adam, we want to optimize a model in a fast and stable way.</p>
<p>The momentum gradient descent ensures the fast part, and the RMSProp ensures the secure part.</p>
<p>Nowadays, for LLMs, which once again are just very big neural network models, a variant of Adam called AdamW is more often used.</p>
<p>Now, let's build a code example of using Adam.</p>
<h4 id="heading-code-example">Code example:</h4>
<p>Using Adam, we are going to optimize this neural network based on fake data.</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765148552889/28101efb-529f-4828-bb7e-adfbf5202d7f.png" alt="Image of a neural network" style="display:block;margin:0 auto" width="655" height="391" loading="lazy">

<p>It will take 4 features:</p>
<ul>
<li><p>Income</p>
</li>
<li><p>Credit score</p>
</li>
<li><p>Debt ratio</p>
</li>
<li><p>Bankruptcy history</p>
</li>
</ul>
<p>And it will tell us if we should or should not approve credit for a given person.</p>
<p>Also, since this book is an introduction to the math of AI, I will not, in this code example, discuss hyperparameter optimization, regularization techniques, and other more advanced topics and good practices.</p>
<p>I want to show why this neural network fails with this data and explain the importance of using great data.</p>
<p>Here is the whole code (and we’ll see each part more in-depth below):</p>
<pre><code class="language-python">import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader, random_split
import pytorch_lightning as pl
import matplotlib.pyplot as plt

torch.manual_seed(42)
x = torch.randn(10000, 4)
y = torch.randint(0, 2, (10000, 1)).float()
dataset = TensorDataset(x, y)

train_size = int(0.8 * len(dataset))
val_size = len(dataset) - train_size
train_dataset, val_dataset = random_split(dataset, [train_size, val_size])

train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=32)

class CreditApprovalNet(pl.LightningModule):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(4, 2)
        self.relu = nn.ReLU()
        self.output = nn.Linear(2, 1)
        self.sigmoid = nn.Sigmoid()
        self.loss_fn = nn.BCELoss()
        self.train_losses = []
    
    def forward(self, x):
        x = self.relu(self.hidden(x))
        return self.sigmoid(self.output(x))
    
    def training_step(self, batch, batch_idx):
        x, y = batch
        y_pred = self(x)
        loss = self.loss_fn(y_pred, y)
        self.log('train_loss', loss)
        self.train_losses.append(loss.item())
        return loss
    
    def configure_optimizers(self):
        return optim.Adam(self.parameters(), lr=0.0001)

model = CreditApprovalNet()
trainer = pl.Trainer(max_epochs=100, logger=False, enable_checkpointing=False)
trainer.fit(model, train_loader, val_loader)

# 
plt.plot(model.train_losses)
plt.xlabel('Training Step')
plt.ylabel('Loss')
plt.title('Credit Approval Training')
plt.grid(True, alpha=0.3)
plt.show()
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765150336432/8bb2eab8-60a1-4a01-babf-1b5b11d9187a.png" alt="Code example of training a neural network - Full code" style="display:block;margin:0 auto" width="3096" height="5252" loading="lazy">

<p>Now let’s break it down:</p>
<p><strong>Importing libraries:</strong></p>
<pre><code class="language-python">import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader, random_split
import pytorch_lightning as pl
import matplotlib.pyplot as plt
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765151014087/80097a4b-6bf2-4af0-94da-7f929cf35d2c.png" alt="Code example of training a neural network - Importing libraries" style="display:block;margin:0 auto" width="2732" height="932" loading="lazy">

<p>In this block of code, we are importing code from 3 Python libraries:</p>
<ul>
<li><p><a href="https://pytorch.org/">PyTorch</a>: One of the most popular python libraries to create new AI models in AI research</p>
</li>
<li><p><a href="https://lightning.ai/docs/pytorch/stable/">PyTorch Lightning</a>: A PyTorch wrapper that organizes training code and handles repetitive tasks automatically</p>
</li>
<li><p><a href="https://matplotlib.org/">Matplotlib</a>: One of the most popular python libraries to make graphs from data</p>
</li>
</ul>
<p><strong>Creating data:</strong></p>
<pre><code class="language-python">torch.manual_seed(42)
x = torch.randn(10000, 4)
y = torch.randint(0, 2, (10000, 1)).float()
dataset = TensorDataset(x, y)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765151040691/a2405e15-8ed0-4988-8b78-724f1bd60347.png" alt="Code example of training a neural network - creating data" style="display:block;margin:0 auto" width="2080" height="752" loading="lazy">

<p>In this part, we define a seed to make the random numbers reproducible. In other words, when we run the code many times, the same random numbers will be generated.</p>
<p>Next, we will create 10,000 applications for credit with 4 features in X and their approval decisions in y. After that, we unify everything in the dataset variable.</p>
<p>We’ll use TensorDataset because it allows us to have the 4 features and the target paired together. This way, the data does not get mixed up during training.</p>
<p><strong>Dividing data:</strong></p>
<pre><code class="language-python">train_size = int(0.8 * len(dataset))
val_size = len(dataset) - train_size
train_dataset, val_dataset = random_split(dataset, [train_size, val_size])
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765151063358/8325f2eb-3cf9-4900-909d-545637e20608.png" alt="Code example of training a neural network - Dividing data" style="display:block;margin:0 auto" width="2988" height="664" loading="lazy">

<p>In this block of code, we divide the data into a training dataset and a validation dataset.</p>
<p>This way, we have one dataset that’s being used to train and find the parameters while comparing results with the validation dataset.</p>
<p>As we can see, 80% of the data will be training data, and 20% of the data will be validation data.</p>
<p><strong>Loading data:</strong></p>
<pre><code class="language-python">train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=32)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765151090966/a80b2483-0bc3-4693-9b58-36765e4b2da2.png" alt="Code example of training a neural network - Loading data" style="display:block;margin:0 auto" width="2768" height="572" loading="lazy">

<p>Here, we load the data into data loaders for the AI model to use.</p>
<p>This way, we have the data automatically split into small batches and shuffled. So instead of processing all 10,000 data points, the model will be trained on one batch, improved, then another batch, then improved again, and so forth. That makes training go faster.</p>
<p><strong>Creating AI model and training process:</strong></p>
<pre><code class="language-python">class CreditApprovalNet(pl.LightningModule):
    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(4, 2)
        self.relu = nn.ReLU()
        self.output = nn.Linear(2, 1)
        self.sigmoid = nn.Sigmoid()
        self.loss_fn = nn.BCELoss()
        self.train_losses = []
    
    def forward(self, x):
        x = self.relu(self.hidden(x))
        return self.sigmoid(self.output(x))
    
    def training_step(self, batch, batch_idx):
        x, y = batch
        y_pred = self(x)
        loss = self.loss_fn(y_pred, y)
        self.log('train_loss', loss)
        self.train_losses.append(loss.item())
        return loss
    
    def configure_optimizers(self):
        return optim.Adam(self.parameters(), lr=0.0001)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765151116959/d75bd178-24bb-4e5d-b043-c504e280f500.png" alt="Code example of training a neural network - Creating AI model and training process" style="display:block;margin:0 auto" width="2296" height="2552" loading="lazy">

<p>This code block appears to be complicated, but let’s see each method block by block:</p>
<ul>
<li><strong>Creating the class with inheritance:</strong></li>
</ul>
<pre><code class="language-python">class CreditApprovalNet(pl.LightningModule):
</code></pre>
<p>This way, in one line, we can import everything we need to define both the model and how it will be trained.</p>
<ul>
<li><strong>init: Builds the model's layers and components:</strong></li>
</ul>
<pre><code class="language-python">    def __init__(self):
        super().__init__()
        self.hidden = nn.Linear(4, 2)
        self.relu = nn.ReLU()
        self.output = nn.Linear(2, 1)
        self.sigmoid = nn.Sigmoid()
        self.loss_fn = nn.BCELoss()
        self.train_losses = []
</code></pre>
<p>In this section of the code, we are defining the architecture of the AI model.</p>
<ul>
<li><strong>forward: Processes input data through the network to make predictions:</strong></li>
</ul>
<pre><code class="language-python">    def forward(self, x):
        x = self.relu(self.hidden(x))
        return self.sigmoid(self.output(x))
</code></pre>
<p>In this part of the code, we are defining how data will flow in the AI model based on the architecture defined.</p>
<ul>
<li><strong>training_step: Calculates loss for each batch during training:</strong></li>
</ul>
<pre><code class="language-python">    def training_step(self, batch, batch_idx):
        x, y = batch
        y_pred = self(x)
        loss = self.loss_fn(y_pred, y)
        self.log('train_loss', loss)
        self.train_losses.append(loss.item())
        return loss
</code></pre>
<p>Here, we are defining how the model will be trained. In other words, how we will find the best parameters for the model to predict well.</p>
<ul>
<li><strong>configure_optimizers: Sets the Adam optimizer with learning rate:</strong></li>
</ul>
<pre><code class="language-python">    def configure_optimizers(self):
        return optim.Adam(self.parameters(), lr=0.0001)
</code></pre>
<p>Finally, here we are defining what optimizer we are going to use to, step by step, improve the AI model parameters.</p>
<p><strong>Training AI model:</strong></p>
<pre><code class="language-python">model = CreditApprovalNet()
trainer = pl.Trainer(max_epochs=100, logger=False, enable_checkpointing=False)
trainer.fit(model, train_loader, val_loader)
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765151149824/33cb6ad3-3a5d-4964-ab45-ccfd68cd0521.png" alt="Code example of training a neural network - Training AI model" style="display:block;margin:0 auto" width="3096" height="752" loading="lazy">

<p>In this block of code:</p>
<ul>
<li><p>We create the neural network model in the first line</p>
</li>
<li><p>In the 2nd and 3rd line, we prepare the training settings and train the model for 100 epochs</p>
</li>
</ul>
<p>This way, in the command line, this appears:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765152230535/3a5a6a13-12b1-4f31-8bec-cfbc830510a6.png" alt="Code example of training a neural network - training an AI model - command line showing number of layers and parameters" style="display:block;margin:0 auto" width="602" height="306" loading="lazy">

<p>The PyTorch code is essentially telling us the number of parameters in the AI model!</p>
<p><strong>Seeing results and understanding why they are not good:</strong></p>
<pre><code class="language-python">
plt.plot(model.train_losses)
plt.xlabel('Training Step')
plt.ylabel('Loss')
plt.title('Credit Approval Training')
plt.grid(True, alpha=0.3)
plt.show()
</code></pre>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765151210074/3cbecda5-616e-4c3b-a942-2512f81697a1.png" alt="Code example of seeing results and understanding why they are not good:" style="display:block;margin:0 auto" width="2080" height="1024" loading="lazy">

<p>Using the Matplotlib library, we plot the results:</p>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765152336092/6cfce900-ffb6-449f-9d5d-827ff71735bb.png" alt="Code example of training a neural network - Plot the training done over time." style="display:block;margin:0 auto" width="1536" height="916" loading="lazy">

<p><strong>The AI model is not converging.</strong></p>
<p>We can see that because the loss is nearly 0.7 (70%) over time.</p>
<p>The main reason the model is not converging well is that there is little to no relationship between the 4 features and the target variable.</p>
<p>In other words, we do not have good data.</p>
<p>The code works perfectly, but this shows the <strong>most important rule in machine learning</strong>: when we create an AI model, the MOST IMPORTANT thing is data.</p>
<p>It does not matter if you use a simple linear regression or a neural network based on transformers or whatever. If you do not have high quality data, the model is not going to perform well.</p>
<p>Even if we use a good optimizer, like Adam, it will not solve the data problem.</p>
<p><strong>Next steps: Common beginner mistakes</strong></p>
<p>I also wrote this exact code example to show you something very important: neural networks are not always the best models to use.</p>
<p>This is a very common beginner mistake. You may start with neural networks for everything, when often machine learning methods with little data preprocessing do the job well.</p>
<p>For this type of problem, the solution is to first try machine learning methods instead of going to neural networks.</p>
<p>There are many reasons for this, but the main ones are:</p>
<ul>
<li><p>Machine learning methods are simpler and often quicker to train than neural networks</p>
</li>
<li><p>Machine learning methods are simpler to understand how they make decisions. In other words, we can understand how the machine learning model thought to make a prediction.</p>
</li>
<li><p>With computational learning, we can guess with certain machine learning models how well they will predict in the future and provide theoretical guarantees about their performance.</p>
</li>
</ul>
<p>Another common mistake is not dividing the data.</p>
<p>To simplify, I created only a training and validation division of the data</p>
<p>In a serious project, you should always divide it into 3 parts: training, validation, and testing.</p>
<p>With training, you create the model. With validation, you test the model based on the data it was trained on. With the test dataset part, you compare if the loss of the model is similar to the validation or different. If they are very different, it means that the AI model converged to the validation dataset but not the test dataset.</p>
<p>I challenge you to think further about how you could improve this code and to try to make the synthetic data more correlated in order to improve its quality.</p>
<h3 id="heading-applications-in-ai-and-control-theory-of-optimization-theory">Applications in AI and Control Theory of&nbsp;Optimization Theory</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765002780396/5aaf78bb-a06a-4d09-b681-a604a323d430.jpeg" alt="Image of a robot hand touching a web" style="display:block;margin:0 auto" width="6177" height="4118" loading="lazy">

<p><a href="https://www.pexels.com/photo/robot-pointing-on-a-wall-8386440/">Photo by Tara Winstead</a></p>
<p>Optimization theory serves as the engine behind AI and control systems that shape our lives.</p>
<p>From unlocking your phone with facial recognition to autopilot systems guiding planes, optimization algorithms are constantly at work.</p>
<p>When you ask ChatGPT a question, optimization theory determines the values of billions of parameters during training.</p>
<p>The same is true for all other LLMs like Gemini, Claude, Grok, DeepSeek, and others. All of them contain millions and millions of parameters. The only way to find the best combination of the parameters to achieve a certain objective is with optimization theory.</p>
<p>In control theory, many systems like Model Predictive Control (MPC) and adaptive control systems only work thanks to optimization methods that balance how internal components of the control system should work together</p>
<p>Beyond training neural networks and controlling physical systems, optimization powers recommendation systems, resource allocation, and so many other systems.</p>
<p>Some examples are:</p>
<ul>
<li><p>Netflix movie recommendation system</p>
</li>
<li><p>Spotify's song suggestion system</p>
</li>
<li><p>Google systems to reduce data center cooling costs</p>
</li>
<li><p>Quantitative trading firms high-frequency trading systems</p>
</li>
</ul>
<p>To end this final chapter, I’ll share this:</p>
<p><strong>It is optimization theory that makes math models into AI models that impact the lives of millions worldwide.</strong></p>
<h2 id="heading-conclusion-where-mathematics-and-ai-meet">Conclusion: Where Mathematics and AI Meet</h2>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1765002962447/8cdbc79a-5d9c-406d-bad6-2f2e49566b36.jpeg" alt="Pyramids of Egypt with a camel sitting" style="display:block;margin:0 auto" width="5563" height="3709" loading="lazy">

<p><a href="https://www.pexels.com/photo/a-camel-lying-in-the-ground-on-the-background-of-pyramids-18991572/">Photo by AXP Photography</a></p>
<p>When ancient civilizations first carved numbers into clay tablets, they likely didn’t imagine that these symbols would one day allow humanity to create the scientific, technological, and medical marvels we have today.</p>
<p>Yet here we are.</p>
<p>We’re in an era where mathematical ideas developed over many centuries – even millennia – have converged to create artificial intelligence.</p>
<p>Throughout this book, we've traced a path from the most basic math concepts to the cutting edge of AI. We have seen how:</p>
<ul>
<li><p>Matrices compress complex systems into simple forms</p>
</li>
<li><p>Derivatives measure change</p>
</li>
<li><p>Probability helps us navigate uncertainty</p>
</li>
<li><p>Optimization guides algorithms toward better decisions to learn faster.</p>
</li>
</ul>
<p>We’ve also learned how each math field has helped create tools that are responsible for many of the things we take for granted today.</p>
<h3 id="heading-mathematics-is-the-foundation-of-ai">Mathematics is the Foundation of AI</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766902825228/e14431de-44da-4e26-a646-5d277c16b073.jpeg" alt="Board with an integral equation in it" style="display:block;margin:0 auto" width="5060" height="3358" loading="lazy">

<p><a href="https://www.pexels.com/photo/person-writing-on-white-board-3781338/">Photo by Jeswin Thomas</a></p>
<p>Always remember this: AI is not pure magic or a "being" we don't understand. It’s just the combination of many math ideas working very well together.</p>
<p>When you ask a question of ChatGPT or any other LLM, it generates a response. And in the process of generating that response, there are millions of matrix multiplications happening in seconds.</p>
<p>Or, for example, when a self-driving car decides to stop moving because it’s coming up to a crosswalk, there are a lot of math computations (related to calculus and probability and statistics) working very fast to ensure safety.</p>
<p>The great thing about mathematics is that it’s a common, standard language of logic. No matter the backgrounds of people or where they were born, a derivative will always be a derivative, and the same thing goes for key AI concepts.</p>
<p>This way, scientists and engineers worldwide can improve each other's work because everyone understands the same language.</p>
<h3 id="heading-the-future-on-device-ai-and-the-democratization-of-ai">The Future: On Device AI and the Democratization of AI</h3>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1766902760109/02b3f00d-a8df-4546-bf41-c1791cdc5f18.jpeg" alt="Image of an chip" style="display:block;margin:0 auto" width="4500" height="3000" loading="lazy">

<p><a href="https://www.pexels.com/photo/abstract-image-of-a-microchip-with-heatmap-colors-28767589/">Photo by Steve Johnson</a></p>
<p>One shift happening now is the move toward edge AI. That is, AI that runs locally on your phone, computer, and really in all your devices (rather than in distant data centers).</p>
<p>This way, privacy is guaranteed because it runs locally. Waiting times for AI models decrease because no data needs to be sent. AI can be used offline, and costs decrease.</p>
<p>And what about the massive data centers being built all over the world? Those will be used for more products that will help improve the lives of millions of people.</p>
<p>As AI becomes more local and more processing power is freed up from big data centers, new AI innovations will appear, and more benefits will come.</p>
<p>The same way that in the past century every computer got its own networking chip, every device will have (and in some cases, already has) AI accelerators.</p>
<p>And much of it will be thanks to the math you learned in this book.</p>
<h3 id="heading-final-reflections">Final Reflections</h3>
<p>Isaac Newton wrote, "If I have seen further, it is by standing on the shoulders of giants."</p>
<p>Every algorithm you use, every model you train, and every new theorem you learn stands on centuries of mathematical progress. You now stand on those same shoulders of these giants!</p>
<p>Thank you for reading, and happy learning.</p>
<p>Here’s the full book <a href="https://github.com/tiagomonteiro0715/The-Math-Behind-Artificial-Intelligence-A-Guide-to-AI-Foundations">GitHub repository with all the code</a>.</p>
<h3 id="heading-acknowledgements">Acknowledgements</h3>
<p>First and foremost, I would like to thank <a href="https://www.linkedin.com/in/guilherme-mendes-a416b7206/"><strong>Guilherme Mendes</strong></a>, currently a Master’s student in Electrical and Computer Engineering at NOVA University, specializing in Control Theory, for reviewing the mathematical and technical details of the 1st version of this book.</p>
<p>I am also grateful to the organizations that gave me opportunities to grow:</p>
<ul>
<li><p><a href="https://www.fct.unl.pt/en">NOVA School of Science and Technology</a></p>
</li>
<li><p><a href="https://ieee-pt.org/">IEEE Portugal Section</a></p>
</li>
<li><p><a href="https://www.siliconvalleyfellowship.com/">Silicon Valley Fellowship</a></p>
</li>
<li><p><a href="https://www.northeastern.edu/">Northeastern University</a></p>
</li>
<li><p><a href="https://best.eu.org/index.jsp">BEST and BEST Almada</a></p>
</li>
<li><p><a href="https://magmastudio.pt/">Magma Studio</a></p>
</li>
</ul>
<p>A special thank you goes to the freeCodeCamp editorial team**,** especially Abigail Rennemeyer, for their patience and for reviewing every chapter of this book.</p>
<p>I would also like to thank all the professors at NOVA FCT who have taught and guided me throughout my academic journey, especially those from the Department of Electrical and Computer Engineering.</p>
<h2 id="heading-about-the-author">About the Author</h2>
<ul>
<li><p>LinkedIn: <a href="https://www.linkedin.com/in/tiago-monteiro-/">https://www.linkedin.com/in/tiago-monteiro-</a></p>
</li>
<li><p>GitHub: <a href="https://github.com/tiagomonteiro0715">https://github.com/tiagomonteiro0715</a></p>
</li>
<li><p>Email: <a href="mailto:monteiro.t@northeastern.edu">monteiro.t@northeastern.edu</a></p>
</li>
</ul>
<p>My name is Tiago Monteiro, and I’m now pursuing a master's degree in Artificial Intelligence at Northeastern University in the Silicon Valley Campus (San Jose) on a merit-based scholarship.</p>
<p>I’m not from the United States. I am a Portuguese national, born and raised in the district of Lisbon.</p>
<p>In Portugal, I completed a bachelor's degree in electrical and computer engineering at NOVA University, one of Portugal's best universities.</p>
<p>I have authored over 20 articles for freeCodeCamp, which have accumulated more than 240,000 views over the years, and completed the Deep Learning Specialization from DeepLearningAI, taught by Andrew Ng.</p>
<p>Also, I had the privilege of participating in the winter 2025 batch of the renowned Silicon Valley Fellowship program.</p>
<h4 id="heading-why-did-i-choose-electrical-and-computer-engineering">Why did I choose electrical and computer engineering?</h4>
<p>After finishing the Portuguese national math exam in 12th grade, I chose Electrical and Computer Engineering (ECE) to challenge myself and learn new math on my own.</p>
<p>The ECE degree combined:</p>
<ul>
<li><p>Advanced Mathematics</p>
</li>
<li><p>Programming (from Assembly to Python)</p>
</li>
<li><p>Physics (classical mechanics, electromagnetism)</p>
</li>
</ul>
<h4 id="heading-what-did-i-gain-exactly">What did I gain exactly?</h4>
<p>I mastered the skills needed to quickly understand AI research, particularly after completing Andrew Ng's Deep Learning Specialization.</p>
<p>In Portugal, I also studied advanced STEM areas including, for example:</p>
<ul>
<li><p><strong>Partial Differential Equations</strong> for modeling real-world phenomena</p>
</li>
<li><p><strong>Harmonic analysis</strong> (Fourier/Laplace transforms) for signal processing and alternative problem perspectives</p>
</li>
<li><p><strong>Complex analysis</strong> involving derivatives and integrals in the complex domain</p>
</li>
<li><p><strong>Numerical methods</strong> for approximating mathematical solutions computationally</p>
</li>
<li><p><strong>Signal/control theory</strong> for ensuring system stability in dynamic environments</p>
</li>
<li><p><strong>Physics classes</strong> in classical mechanics and electromagnetism fundamentals</p>
</li>
</ul>
<p>While not directly applied to AI, these studies enhanced my systems thinking and ability to independently learn complex STEM concepts.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn Discrete Mathematics ]]>
                </title>
                <description>
                    <![CDATA[ Discrete mathematics plays a key role in machine learning and algorithms. You can use it to find the shortest path (graph theory), encrypt files, compress data, and to do many other things. We just posted a discreet mathematics course on the freeCode... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-discrete-mathematics/</link>
                <guid isPermaLink="false">6915ec909bbfb50f5a356a15</guid>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Thu, 13 Nov 2025 14:34:56 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1763044466535/8ae84f6a-dfd2-4988-8f73-77e6f548846b.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Discrete mathematics plays a key role in machine learning and algorithms. You can use it to find the shortest path (graph theory), encrypt files, compress data, and to do many other things.</p>
<p>We just posted a discreet mathematics course on the freeCodeCamp.org YouTube channel. Karol Kurek teaches this course. He is a former math teacher and senior Python developer.</p>
<p>This field is constantly evolving along with the development of its key application: computer science. This course is an introduction to this group of mathematical sciences, and we will focus on the most important issues on which other branches of discrete mathematics are based: combinatorics, number theory, prime numbers, and several selected topics: pigeonhole principle, stars and bars principle, Stirling's number, and the Chinese remainder theorem.</p>
<p>At the end of the course, there are tips and encouragement for further exploration of this field.</p>
<p>Here are the sections in this course:</p>
<ul>
<li><p>Introduction to Discrete Mathematics</p>
</li>
<li><p>Permutations: Definition and Examples</p>
</li>
<li><p>Applications of Permutations</p>
</li>
<li><p>Cycles and Multiset Permutations</p>
</li>
<li><p>Counting Permutations: The Formulas</p>
</li>
<li><p>Permutations in Python with itertools</p>
</li>
<li><p>Custom Python Function for Counting Permutations</p>
</li>
<li><p>Heap's Algorithm</p>
</li>
<li><p>K-Permutations and K-Tuples</p>
</li>
<li><p>The Rule of Product</p>
</li>
<li><p>The Rule of Sum</p>
</li>
<li><p>Exercises: Rule of Product &amp; Sum</p>
</li>
<li><p>The Inclusion-Exclusion Principle</p>
</li>
<li><p>Exercises: Inclusion-Exclusion Principle</p>
</li>
<li><p>Mathematical Notations (Sigma &amp; Pi)</p>
</li>
<li><p>Equinumerosity &amp; Countable Sets</p>
</li>
<li><p>Proving Rational Numbers are Countable</p>
</li>
<li><p>Prime Numbers &amp; Sieve of Eratosthenes</p>
</li>
<li><p>Prime Number Generation in Python</p>
</li>
<li><p>Advanced Properties of Prime Numbers</p>
</li>
<li><p>GCD &amp; LCM (Greatest Common Divisor &amp; Least Common Multiple)</p>
</li>
<li><p>Co-prime Numbers</p>
</li>
<li><p>Congruences (Modular Arithmetic)</p>
</li>
<li><p>Binomial Coefficients &amp; Pascal's Triangle</p>
</li>
<li><p>Combinations</p>
</li>
<li><p>Solving a Complex Combinatorics Problem</p>
</li>
<li><p>Stirling Numbers</p>
</li>
<li><p>Bell Numbers</p>
</li>
<li><p>The Chinese Remainder Theorem</p>
</li>
<li><p>Conclusion &amp; What's Next</p>
</li>
</ul>
<p>Watch the full course <a target="_blank" href="https://youtu.be/G9JxuWk7BDA">on the freeCodeCamp.org YouTube channel</a> (9-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/G9JxuWk7BDA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How Does Cosine Similarity Work? The Math Behind LLMs Explained ]]>
                </title>
                <description>
                    <![CDATA[ When you talk to a large language model (LLM), it feels like the model understands meaning. But under the hood, the system relies on numbers, vectors, and math to find the relationships between words and sentences. One of the most important tools tha... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-does-cosine-similarity-work/</link>
                <guid isPermaLink="false">68cb5c87b90c34f2f16f5703</guid>
                
                    <category>
                        <![CDATA[ LLM&#39;s  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ llm ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Math ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Manish Shivanandhan ]]>
                </dc:creator>
                <pubDate>Thu, 18 Sep 2025 01:12:39 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1758157868492/7d242ca0-7721-4d25-93fb-2f0a6e319690.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you talk to a large language model (LLM), it feels like the model understands meaning. But under the hood, the system relies on numbers, vectors, and math to find the <a target="_blank" href="https://www.turingtalks.ai/p/how-to-perform-sentence-similarity-check-using-sentence-transformers">relationships between words</a> and sentences.</p>
<p>One of the most important tools that makes this possible is cosine similarity. If you want to know how an LLM can judge that two sentences mean almost the same thing, cosine similarity is the key.</p>
<p>This article explains cosine similarity in plain language, shows the math behind it, and connects it to the way modern language models work. By the end, you will see why this simple idea of measuring angles between vectors powers search, chatbots, and many other AI systems.</p>
<h2 id="heading-table-of-contents"><strong>Table of Contents</strong></h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-what-is-cosine-similarity">What Is Cosine Similarity</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-math-behind-cosine-similarity">The Math Behind Cosine Similarity</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-a-simple-example">A Simple Example</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-cosine-similarity-in-embeddings">Cosine Similarity in Embeddings</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-llms-use-cosine-similarity">How LLMs Use Cosine Similarity</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-limits-of-cosine-similarity">Limits of Cosine Similarity</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-why-it-matters-for-llms">Why It Matters for LLMs</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-what-is-cosine-similarity"><strong>What Is Cosine Similarity?</strong></h2>
<p>Imagine you have two sentences. To a computer, they are not words but vectors, a long lists of numbers that capture meaning.</p>
<p>Cosine similarity measures how close these two vectors are, not by their length, but by the angle between them.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1757481567784/ccd74ce9-d561-47b5-a5e8-c3e77ed36e79.png" alt="Cosine Similarity" class="image--center mx-auto" width="659" height="342" loading="lazy"></p>
<p>Think of two arrows starting from the same point. If they point in the same direction, the angle between them is zero, and cosine similarity is one. If they point in opposite directions, the angle is 180 degrees, and cosine similarity is negative one. If they are at a right angle, the cosine similarity is zero.</p>
<p>So, cosine similarity tells us whether two vectors are pointing in the same general direction. In language tasks, this means it tells us whether two pieces of text carry a similar meaning.</p>
<h2 id="heading-the-math-behind-cosine-similarity"><strong>The Math Behind Cosine Similarity</strong></h2>
<p>To understand cosine similarity, we need to look at a bit of math. The cosine of an angle in geometry is the ratio between the dot product of two vectors and the product of their magnitudes. Written as a formula, cosine similarity looks like this:</p>
<pre><code class="lang-plaintext">cosine_similarity(A, B) = (A · B) / (||A|| * ||B||)
</code></pre>
<p>Here:</p>
<ul>
<li><p><code>A · B</code> is the dot product of vectors A and B.</p>
</li>
<li><p><code>||A||</code> is the magnitude (length) of vector A.</p>
</li>
<li><p><code>||B||</code> is the magnitude of vector B.</p>
</li>
</ul>
<p>The dot product multiplies corresponding numbers in the two vectors and adds them up. The magnitude of a vector is like finding the length of an arrow, using the <a target="_blank" href="https://en.wikipedia.org/wiki/Pythagorean_theorem">Pythagorean theorem</a>.</p>
<p>This formula always gives a value between -1 and 1. A value close to 1 means the vectors are pointing in nearly the same direction. A value close to 0 means they are unrelated. A value close to -1 means they are opposite.</p>
<h2 id="heading-a-simple-example"><strong>A Simple Example</strong></h2>
<p>Let’s see a short example using Python. Suppose you want to check how similar two short texts are. We can use <a target="_blank" href="https://scikit-learn.org/">scikit-learn</a> to turn them into vectors and then compute cosine similarity.</p>
<pre><code class="lang-plaintext">from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

texts = [
    "I love machine learning",
    "I love deep learning"
]

vectorizer = TfidfVectorizer().fit_transform(texts)
vectors = vectorizer.toarray()

similarity = cosine_similarity([vectors[0]], [vectors[1]])
print("Cosine similarity:", similarity[0][0])
</code></pre>
<p>The code starts by importing two important tools. <code>TfidfVectorizer</code> is responsible for turning text into numbers, while <code>cosine_similarity</code> measures how similar two sets of numbers are. Together, they let us compare text in a way a computer can understand.</p>
<p>Next, we define the sentences we want to compare. In this example, we use “I love machine learning” and “I love deep learning.” These two sentences share some words such as “I,” “love,” and “learning,” while differing in one word: “machine” versus “deep.” This makes them good examples to test, because they are clearly related but not exactly the same.</p>
<p>The vectorizer then builds a vocabulary from all the unique words across the two sentences. For these inputs, the vocabulary becomes <code>["deep", "learning", "love", "machine"]</code>. This means the program now has a list of all the words it will track when building the numerical representation of the sentences.</p>
<p>Each sentence is then converted into a vector, which is simply a list of numbers. These numbers are not just raw word counts. Instead, they are weighted using TF-IDF, which stands for <a target="_blank" href="https://www.learndatasci.com/glossary/tf-idf-term-frequency-inverse-document-frequency/">Term Frequency–Inverse Document Frequency</a>.</p>
<p>TF-IDF gives more importance to words that matter in a sentence and less importance to very common words. In simplified form, the first sentence becomes something like <code>[0. 0.50154891 0.50154891 0.70490949]</code>, while the second becomes <code>[0.70490949 0.50154891 0.50154891 0. ]</code>. The numbers may look small, but what matters is their relative values.</p>
<p>The <code>.toarray()</code> method then converts these vectors into standard Python arrays. This makes them easier to handle, since the TF-IDF output is stored in a special sparse format by default.</p>
<p>Once the sentences are represented as vectors, cosine similarity is applied. This step checks the angle between the two vectors.</p>
<p>If the vectors point in exactly the same direction, their similarity score will be one. If they are unrelated, the score will be close to zero. If they point in opposite directions, the score will be negative.</p>
<p>In this case, because the two sentences share most of their words, the vectors point in a similar direction, so the cosine similarity falls somewhere around 0.5 to 0.7.</p>
<p>In simple terms, this code shows how a computer can compare two sentences by turning them into vectors of numbers and then checking how close those vectors are. By using cosine similarity, the program can judge not just whether the sentences share words, but also how strongly they overlap in meaning.</p>
<h2 id="heading-cosine-similarity-in-embeddings"><strong>Cosine Similarity in Embeddings</strong></h2>
<p>In practice, LLMs like GPT or BERT do not use simple word counts. Instead, they use embeddings.</p>
<p>An <a target="_blank" href="https://www.freecodecamp.org/news/how-ai-agents-remember-things-vector-stores-in-llm-memory/">embedding</a> is a dense vector that captures meaning. Each word, phrase, or sentence is turned into a set of numbers that place it in a high-dimensional space.</p>
<p>In this space, words with similar meaning are close together. For example, the embeddings for “king” and “queen” are closer than the embeddings for “king” and “table.”</p>
<p>Cosine similarity is the tool that allows us to measure how close two embeddings are. When you search for “dog,” the system can look for embeddings that point in a similar direction. That way, it finds results about “puppy,” “canine,” or “pet” even if those exact words are not in your query.</p>
<h2 id="heading-how-llms-use-cosine-similarity">How LLMs Use Cosine Similarity</h2>
<p>Large language models use cosine similarity in many ways. When you ask a question, the model encodes your input into a vector. It then compares this vector with stored knowledge or with candidate answers using cosine similarity.</p>
<p>For semantic search, cosine similarity helps rank documents. A system can embed all documents into vectors, then embed your query and compute similarity scores. The documents with the highest scores are the most relevant.</p>
<p>In clustering, cosine similarity helps group sentences that have related meaning. In recommendation systems, it helps match users to items by comparing their preference vectors.</p>
<p>Even when generating answers, LLMs rely on vector similarity to decide which words or phrases best follow in context. Cosine similarity gives the model a simple but powerful way to measure closeness of meaning.</p>
<h2 id="heading-limits-of-cosine-similarity"><strong>Limits of Cosine Similarity</strong></h2>
<p>While cosine similarity is powerful, it has limits. It depends heavily on the quality of embeddings. If embeddings fail to capture meaning well, similarity scores may not reflect real-world closeness.</p>
<p>Also, cosine similarity only measures direction. Sometimes, magnitude contains useful information too. For example, a sentence embedding might have a length that reflects confidence. By ignoring it, cosine similarity may lose part of the picture.</p>
<p>Still, despite these limits, cosine similarity remains one of the most widely used methods in natural language processing.</p>
<h2 id="heading-why-it-matters-for-llms"><strong>Why It Matters for LLMs</strong></h2>
<p>Cosine similarity is not just a math trick. It is a bridge between human language and machine understanding. It allows a model to treat meaning as geometry, turning questions and answers into points in space.</p>
<p>Without cosine similarity, embeddings would be less useful, and tasks like semantic search, clustering, and ranking would be harder. By reducing the problem to measuring angles, we make meaning measurable and usable.</p>
<p>Every time you search on Google, chat with an AI, or use a recommendation engine, cosine similarity is at work behind the scenes.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Cosine similarity explains how LLMs judge the closeness of meaning between words, sentences, or even whole documents. It works by comparing the angle between vectors, not their length, which makes it ideal for text. With embeddings, cosine similarity becomes the foundation of semantic search, clustering, recommendations, and many other tasks in natural language processing.</p>
<p>The next time an AI gives you an answer that feels “close enough,” remember that a simple mathematical idea, measuring the angle between two arrows, is doing much of the heavy lifting.</p>
<p><em>Hope you enjoyed this article. Signup for my free AI newsletter</em> <a target="_blank" href="https://www.turingtalks.ai/"><strong><em>TuringTalks.ai</em></strong></a> <em>for more hands-on tutorials on AI. You can also find</em> <a target="_blank" href="https://manishshivanandhan.com/"><strong><em>visit my website</em></strong></a><em>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Write a PHP Script to Calculate the Area of a Triangle ]]>
                </title>
                <description>
                    <![CDATA[ In programming, being able to find the area of a triangle is useful for many reasons. It can help you understand logic-building and syntax, and it’s a common programming problem used in school assignments. There are also many real-world applications,... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/use-php-to-calculate-the-area-of-a-triangle/</link>
                <guid isPermaLink="false">68542db2cefef90e768ba1fe</guid>
                
                    <category>
                        <![CDATA[ PHP ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ triangle ]]>
                    </category>
                
                    <category>
                        <![CDATA[ DSA ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Programming Blogs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ MathJax ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ AYUSH MISHRA ]]>
                </dc:creator>
                <pubDate>Thu, 19 Jun 2025 15:33:06 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1750346934679/2e46bebb-9614-4f1a-afb5-9bbe27906b4e.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In programming, being able to find the area of a triangle is useful for many reasons. It can help you understand logic-building and syntax, and it’s a common programming problem used in school assignments. There are also many real-world applications, such as computer graphics, geometry-based simulations, or construction-related calculations.</p>
<p>In this article, we’ll look at a common problem: we are given the dimensions of a triangle, and our task is to calculate its area. You can calculate the area of a triangle using different formulas, depending on the information you have about the triangle. Here, you’re going to learn how to do it using PHP.</p>
<h3 id="heading-after-reading-this-tutorial">After reading this tutorial:</h3>
<ul>
<li><p>You will understand the basic logic behind calculating the area of a triangle.</p>
</li>
<li><p>You will know how to write PHP code that calculates the triangle’s area using pre-defined and user-entered values.</p>
</li>
<li><p>You will know how to apply this logic in small projects and assignments.</p>
</li>
</ul>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-prerequisites">Prerequisites</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-find-the-area-of-a-triangle-using-direct-formulas">Find the Area of a Triangle Using Direct Formulas</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-find-the-area-of-a-triangle-using-the-base-and-height-approach">Find the Area of a Triangle Using the Base and Height Approach</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-find-the-area-of-a-triangle-using-herons-formula">Find the Area of a Triangle Using Heron's Formula</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-find-the-area-of-a-triangle-using-two-sides-and-included-angle-trigonometric-formula">Find the Area of a Triangle Using Two Sides and Included Angle (Trigonometric Formula)</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ol>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>You’ll understand this guide more easily if you have some knowledge about a few things:</p>
<h3 id="heading-basic-php">Basic PHP</h3>
<p>You’ll need to know basic PHP syntax to fully understand the problem. If you know how to write a simple echo statement or create a variable in PHP, then you should be good to go.</p>
<h3 id="heading-local-php-environment">Local PHP Environment</h3>
<p>To run the PHP code successfully, you should have local PHP development, such as XAMPP or WAMP, on your machine. You can also use online PHP editors like PHP Fiddle or OnlineGDB to run a PHP script without any installation.</p>
<p>In this tutorial we are going to explore three approaches to determine the area of the triangle in PHP based on the amount of information available about the triangle.</p>
<ul>
<li><p><strong>Base and Height Formula Approach:</strong> This approach is applicable when you have the perpendicular height from the base and length of the base in the problem.</p>
</li>
<li><p><strong>Heron’s Formula:</strong> This approach is used to calculate the area of triangle when you have the lengths of all three sides of the triangle.</p>
</li>
<li><p><strong>Trigonometric Formula Approach:</strong> This approach is applied on the problem when you have the length of two sides and the included angle between them.</p>
</li>
</ul>
<p>First, let’s go back to math class and use some direct formulas to find the area.</p>
<h2 id="heading-find-the-area-of-a-triangle-using-direct-formulas">Find the Area of a Triangle Using Direct Formulas</h2>
<h3 id="heading-example-1">Example 1:</h3>
<p>In this first example, you’re given the input base and height of a triangle. You have to return the area of the triangle. For this example, you’ll use a direct formula to calculate the area of the triangle.</p>
<p><strong>Input:</strong></p>
<p>Base = 5,</p>
<p>Height = 10</p>
<p>You can calculate the area of the triangle using the formula:</p>
<p>$$Area = (Base * Height) / 2$$</p><p>So, if you plug in the values you have, you get: (5* 10) / 2 = 25.</p>
<p><strong>Output:</strong></p>
<p>Area = 25</p>
<h3 id="heading-example-2">Example 2:</h3>
<p>In this second example, you’re given the length of two sides of a triangle and one angle between them. You have to return the area of the triangle. In this example, you’ll use another direct formula to calculate the area of the triangle.</p>
<p><strong>Input:</strong></p>
<p>Side A = 7, Side B = 9, Angle between them = 60°</p>
<p>In this case, you’ll use the formula:</p>
<p>$$Area = (1/2) A B * sin(Angle).$$</p><p>Then just substitute in the values you’ve been given to find the area.</p>
<p><strong>Output:</strong></p>
<p>Area = 27.33 (approximately)</p>
<p>Now let’s look at some different approaches to finding the area of a triangle using PHP.</p>
<h2 id="heading-find-the-area-of-a-triangle-using-the-base-and-height-approach">Find the Area of a Triangle Using the Base and Height Approach</h2>
<p>This is the simplest and most direct approach for calculating the area of a triangle when you know the base and height. In this approach, you’ll directly put values in the formula and find the area of the triangle – but you’ll do it with PHP code.</p>
<p>First, define the base and height of the triangle. Then apply the formula for the area of the triangle. As we saw above, the formula for the area of a triangle is:</p>
<p>$$Area = (Base * Height) / 2$$</p><p>After calculating the area of the triangle, output the answer.</p>
<p>Alright, so here’s how we can implement that in PHP:</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-comment">// Define the base and height</span>
$base = <span class="hljs-number">5</span>;
$height = <span class="hljs-number">10</span>;

<span class="hljs-comment">// Calculate the area</span>
$area = ($base * $height) / <span class="hljs-number">2</span>;

<span class="hljs-comment">// Output the result</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"The area of the triangle is: "</span> . $area . <span class="hljs-string">" square units."</span>;
<span class="hljs-meta">?&gt;</span>
</code></pre>
<p>Output:</p>
<p>The area of the triangle is 25 square units.</p>
<p>In the above code, first we initialize the base and height of triangle in two variables. Then we plug those values into the area formula. PHP calculates the area of the triangle and displays the answer.</p>
<p><strong>Time Complexity</strong>: In the above approach, we are using the direct formula to calculate and return the area of the triangle, so the time complexity will be constant at O(1). The constant time complexity is efficient as it will remain constant, regardless of the size or values of the base and height.</p>
<p><strong>Space Complexity</strong>: The Space Complexity will be O(1). The space used by the above program is constant, which ensures minimal use of memory. This space complexity is ideal in environments where memory efficiency is a priority.</p>
<p>We use the above approach when we have the length of the base and height of the triangle (whether directly given or easily measurable in a right angle triangle). This method works best for right-angled triangles.</p>
<h2 id="heading-find-the-area-of-a-triangle-using-herons-formula">Find the Area of a Triangle Using Heron's Formula</h2>
<p>Heron’s formula is named after a Greek mathematician named Heron of Alexandria. Heron’s formula is useful when you know the lengths of all three sides of the triangle and you want to calculate the area without needing the height. This formula works for any type of triangle, including scalene triangles (triangles with sides of all different lengths).</p>
<p>Here’s Heron’s formula to calculate the area of a triangle:</p>
<p>$$√s(s−a)(s−b)(s−c) ​$$</p><p>Where:</p>
<ul>
<li><p>s = semi-perimeter = (a+b+c)/2 is the semi-perimeter of the triangle.</p>
</li>
<li><p>a, b, and c are the lengths of the sides.</p>
</li>
</ul>
<p>First, we define the three sides of the triangle. Then, we check all three conditions of the <a target="_blank" href="https://en.wikipedia.org/wiki/Triangle_inequality">Triangle Inequality Theorem</a> which states that if the sum of two sides is greater than the third side, then it is a valid triangle, and the given sides can form a triangle.</p>
<p>We can calculate the semi-perimeter of the triangle using the formula s = a+b+c/2. Then we can apply Heron's formula to calculate the area. After calculating the area, then output the answer.</p>
<p>Here’s how you can implement this in PHP:</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-comment">// Define the sides of the triangle</span>
$a = <span class="hljs-number">7</span>;
$b = <span class="hljs-number">9</span>;
$c = <span class="hljs-number">10</span>;

<span class="hljs-comment">// Check if the sides form a valid triangle using the Triangle Inequality Theorem</span>
<span class="hljs-keyword">if</span> (($a + $b &gt; $c) &amp;&amp; ($a + $c &gt; $b) &amp;&amp; ($b + $c &gt; $a)) {

    <span class="hljs-comment">// Calculate the semi-perimeter</span>
    $s = ($a + $b + $c) / <span class="hljs-number">2</span>;

    <span class="hljs-comment">// Calculate the area using Heron's formula</span>
    $area = sqrt($s * ($s - $a) * ($s - $b) * ($s - $c));

    <span class="hljs-comment">// Output the result</span>
    <span class="hljs-keyword">echo</span> <span class="hljs-string">"The area of the triangle is: "</span> . $area . <span class="hljs-string">" square units."</span>;

} <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// If the sides can't form a valid triangle</span>
    <span class="hljs-keyword">echo</span> <span class="hljs-string">"The given sides do not form a valid triangle."</span>;
}
<span class="hljs-meta">?&gt;</span>
</code></pre>
<p>Output:</p>
<p>The area of the triangle is: 27.321 square units.</p>
<p>In the above code, we first create three variables to store the lengths of the triangle’s sides, and check if the given sides form a valid triangle or not using the Triangle Inequality Theorem. Then we calculate the semi-perimeter using the formula: s = a + b + c / 2. We put the value of the semi-perimeter and lengths of all sides in Heron’s formula to calculate the area. The area of triangle is returned after calculating using the formula.</p>
<p><strong>Time Complexity</strong>: There is a total fixed number of operations such as addition, subtraction, multiplication, and square root. These operations don’t depend on input size as they are performed only a fixed number of times. This means that the time complexity is constant O(1).</p>
<p><strong>Space Complexity</strong>: We have used a fixed number of variables to calculate the area of the triangle. We have not used any additional data structures such as arrays or objects. The memory usage in the program is constant, which is better for low-memory environments. The space complexity is constant O(1).</p>
<p>This approach works best when the lengths of all sides are given. This approach is used mainly for scalene or isosceles triangles where height is directly not given. This approach can work for any type of triangle, however – scalene, isosceles, or equilateral.</p>
<h2 id="heading-find-the-area-of-a-triangle-using-two-sides-and-included-angle-trigonometric-formula">Find the Area of a Triangle Using Two Sides and Included Angle (Trigonometric Formula)</h2>
<p>In this approach, we will see a different variation of the problem. When you know two sides of a triangle and the included angle between them, you can calculate the area using this formula:</p>
<p>$$Area = 1/2 × a × b × sin(θ)$$</p><p>Where:</p>
<ul>
<li><p>a and b are the lengths of the two sides.</p>
</li>
<li><p>θ is the included angle between the two sides, measured in degrees or radians.</p>
</li>
</ul>
<p>Using the above formula, you can calculate the area of a triangle without needing its height. First, you define the two sides of the triangle and the angle between them. Then you convert the angle from degrees to radians if needed (in PHP, you can use deg2rad() to convert degrees to radians). Then you apply the formula.</p>
<p>After calculating the area of the triangle, output the result.</p>
<p>Here’s how to implement this in PHP:</p>
<pre><code class="lang-php"><span class="hljs-meta">&lt;?php</span>
<span class="hljs-comment">// Define the two sides and the included angle</span>
$a = <span class="hljs-number">7</span>;
$b = <span class="hljs-number">9</span>;
$angle = <span class="hljs-number">60</span>; <span class="hljs-comment">// Angle in degrees</span>

<span class="hljs-comment">// Convert the angle from degrees to radians</span>
$angle_in_radians = deg2rad($angle);

<span class="hljs-comment">// Calculate the area using the formula</span>
$area = <span class="hljs-number">0.5</span> * $a * $b * sin($angle_in_radians);

<span class="hljs-comment">// Output the result</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"The area of the triangle is: "</span> . $area . <span class="hljs-string">" square units."</span>;
<span class="hljs-meta">?&gt;</span>
</code></pre>
<p>Output:</p>
<p>The area of the triangle is: 27.321 square units.</p>
<p>Explanation:</p>
<p>In the above case, we’re using the formula:</p>
<p>Area of Triangle = 1/2 × a × b × sin(θ)</p>
<p>And we’re substituting the following values into the formula:</p>
<p>Area= 1/2 × 7 × 9 × sin(60 ∘) ≈ 27.321</p>
<p>In the code, we declared two variables to store the length of the two sides of the triangle, and the variable <code>$angle</code> hold the included angle in degrees. We used <code>deg2rad()</code>, a PHP built-in function which converts an angle from degrees to radians. Then, we applied the actual formula: Area = 1/2 × 7 × 9 × sin(60 ∘). PHP stores the final answer in the <code>$area</code> variable.</p>
<p><strong>Time Complexity</strong>: We are using the direct formula to calculate the area of a triangle when the length of two sides and the angle between them are given. The constant time complexity is O(1).</p>
<p><strong>Space Complexity</strong>: Similarly, it does not take any extra space or use any data structures. It uses a single variable to store the result, which is why the space complexity is constant O(1).</p>
<p>This approach is perfect for the problem in which two sides and the included angle (angle between those sides) are known. You can use it when you cannot easily calculate the height of the triangle. This problem has real-life applications in geometry problems, CAD applications, or physics simulations. This method is very accurate and doesn’t require the length of all sides.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you’ve learned how you can calculate the area of a triangle, both manually and using PHP. You have seen different approaches and learned about which one is best given the information you have. First, we discussed the base and height approach, then looked at Heron’s formula, and finally examined how to handle things when two sides and the included angle are given.</p>
<p>Understanding the logic behind each of these approaches helps you choose the right one based on the given data.</p>
<p>And if you'd like to support me and my work directly so I can keep creating these tutorials, <a target="_blank" href="https://paypal.me/ayushM010">you can do so here</a>. Thank you!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The Architecture of Mathematics – And How Developers Can Use it in Code ]]>
                </title>
                <description>
                    <![CDATA[ "To understand is to perceive patterns." - Isaiah Berlin Math is not just numbers. It is the science of finding complex patterns that shape our world. This means that to truly understand it, we need to see beyond numbers, formulas, and theorems and ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-architecture-of-mathematics-and-how-developers-can-use-it-in-code/</link>
                <guid isPermaLink="false">68308ee8ccde6bc325c82393</guid>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Math ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Machine Learning ]]>
                    </category>
                
                    <category>
                        <![CDATA[ history ]]>
                    </category>
                
                    <category>
                        <![CDATA[ MathJax ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Tiago Capelo Monteiro ]]>
                </dc:creator>
                <pubDate>Fri, 23 May 2025 15:06:16 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1748012748947/1df613bf-93e7-4f03-b0f0-47ff49f38504.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <blockquote>
<p>"To understand is to perceive patterns." - Isaiah Berlin</p>
</blockquote>
<p>Math is not just numbers. It is the science of finding complex patterns that shape our world. This means that to truly understand it, we need to see beyond numbers, formulas, and theorems and understand its structures.</p>
<p>The main goal of this article is to show how math is just like a growing tree of ideas. I want to show that math is a living system of logic, not just formulas to memorize. With analogies, history, and code examples, I want to help you understand math more deeply and how you can apply it to programming.</p>
<p>I’ve also included some code examples here to help you connect theory and practice. I show them to demonstrate how math ideas are applied to real problems. Whether you are new to more advanced math or are more experienced, these code examples will help you understand how to apply math in programming.</p>
<p>This link across theory and application reflects my own studies. I am a finalist in an undergraduate degree in Electrical and Computer Engineering at NOVA FCT, one of the best engineering faculties in Portugal.</p>
<p>My engineering degree is one with more math and physics. This is because it’s key to get a solid grasp of math to understand electronics, telecommunications, control theory, and other areas of engineering.</p>
<p>Here’s a brief overview of some of the math and physics subjects I’ve learned:</p>
<ul>
<li><p><strong>Partial Differential Equations (PDEs):</strong> These equations model real-world phenomena, from heat diffusion to the economy of a country.</p>
</li>
<li><p><strong>Harmonic Analysis (Fourier &amp; Laplace):</strong> Integral transforms like the Fourier and Laplace transform allow us to understand problems in new domains.</p>
</li>
<li><p><strong>Complex Analysis:</strong> Extending calculus into the complex plane gives rise to powerful tools used in physics and engineering.</p>
</li>
<li><p><strong>Numerical Analysis:</strong> When analytical solutions are impossible or inefficient, numerical methods provide computer-based approximations. This is crucial for real-world applications.</p>
</li>
<li><p><strong>Control and Signal Theory:</strong> These areas show us how to design stable systems like rockets, trains, and robots.</p>
</li>
<li><p><strong>Physics:</strong> Courses in Classical Mechanics and Electromagnetism helped bridge theoretical math to physical laws</p>
</li>
</ul>
<p>During my years of study, besides technical skills, I’ve developed a deeper understanding of how the world works and the structure of the field of mathematics. And I’ve started to find patterns in how math is a framework of interconnected logic.</p>
<h3 id="heading-in-this-article-well-explore">In this article, we’ll explore:</h3>
<ul>
<li><p><a class="post-section-overview" href="#heading-simple-analogy-the-tree-of-mathematics">Simple Analogy: The Tree of Mathematics</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-structure-and-history-of-mathematics">The Structure and History of Mathematics</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-an-tree-example-foundations-of-relativity-by-albert-einstein">An Tree example: Foundations of Relativity by Albert Einstein</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-biggest-paradox-of-math-discovered-by-kurt-godel">The Biggest Paradox of Math, Discovered by Kurt Gödel</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-what-about-applied-math-and-engineering">What About Applied Math and Engineering?</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-code-examples-analytical-and-numerical-approaches">Code Examples – Analytical and Numerical Approaches</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-impact-of-a-grand-unified-theory-of-mathematics">The Impact of a Grand Unified Theory of Mathematics</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-a-final-lesson-from-history">A Final Lesson From History</a></p>
</li>
</ul>
<h2 id="heading-simple-analogy-the-tree-of-mathematics">Simple Analogy: The Tree of Mathematics</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747518175609/78838825-d872-42df-9dc8-736fa012a630.jpeg" alt="Photo of two trees by Johannes Plenio: https://www.pexels.com/photo/two-brown-trees-1632790/" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>Imagine math as a vast tree growing forever.</p>
<p>The roots of the tree are the foundations of mathematics: logic and set theory. From this foundation emerge the main basic fields of math: arithmetic, algebra, geometry, and analysis.</p>
<p>As the tree divides further and further into more branches, new, more complex subfields start to appear, like topology, abstract algebra, and complex analysis. Sometimes the branches are connected to each other.</p>
<p>And remember: this tree is always growing in many directions. From branches creating new branches to branches connecting to other branches. Little by little, it grows.</p>
<p>Throughout history, there have been times that, due to some big scientific discoveries, parts of the math tree started to grow very fast. Other times, decades and even centuries passed without many new branches. This is the case for imaginary numbers, for example.</p>
<p>And you might wonder: How many more branches and connections between them will keep appearing?</p>
<h2 id="heading-the-structure-and-history-of-mathematics">The Structure and History of Mathematics</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747518363058/9911acd4-ad4f-4da2-a62b-9fa87e219c35.jpeg" alt="Photo of a writing desk and notebook on Pixabay: https://www.pexels.com/photo/brown-wooden-desk-159618/" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>The first mathematical ideas appeared independently across ancient civilizations. For example:</p>
<ul>
<li><p>India’s invention of zero</p>
</li>
<li><p>Islamic algebraic advances</p>
</li>
<li><p>Greek geometric rigor</p>
</li>
</ul>
<p>Over time, many different great mathematicians created and shared them by writing and giving lectures.</p>
<p>Eventually, these new ideas were shared widely with new generations and these new generations created new math based on old math.</p>
<p>This is is how new branches are continuously born from previous branches of the tree of mathematics.</p>
<p>And this is why Isaac Newton wrote, in a letter to Robert Hooke in 1675:</p>
<blockquote>
<p>If I have seen further, it is by standing on the shoulders of giants</p>
</blockquote>
<p>He meant that by working from previous knowledge, he was able to create and (re)discover new ideas.</p>
<p>Yet, the real power of math lies in practicing it over and over and understanding it more and more deeply. As one of my professors once explained:</p>
<blockquote>
<p><em>More important than knowing the theorems is knowing the ideas behind them and the history of how they were created.</em></p>
</blockquote>
<p>Very often, to solve problems, it is necessary to think in terms of first principles and build from there. Math teaches exactly that. In this way, math is not just an academic subject. It is a language spoken by scientists and engineers around the globe.</p>
<p>By having it well preserved and shared, it is still possible to create new math from previous ideas. And it’s possible for the big tree to continue growing based on previous branches or nodes.</p>
<h2 id="heading-an-tree-example-foundations-of-relativity-by-albert-einstein">An Tree example: Foundations of Relativity by Albert Einstein</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747518865627/e84ff108-b383-405b-8bb0-73ffb50b4dcf.jpeg" alt="Albert Einstein, one of the greatest physics giants in history" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>Albert Einstein created the general and special theories of relativity. These have big consequences nowadays:</p>
<ul>
<li><p>GPS and Global Communication</p>
</li>
<li><p>Advancements in Satellite Telecommunications</p>
</li>
<li><p>Space Exploration and Satellite Launches</p>
</li>
</ul>
<p>But this was only possible through the unification of geometry with calculus, called <strong>differential geometry.</strong> The evolution of differential geometry happened over the centuries, thanks to many great mathematicians. Below are some of them, but this is not a complete list:</p>
<ul>
<li><p><strong>Euclid (circa 300 BCE):</strong> Contributed to geometry, laying the groundwork for later mathematical systems</p>
</li>
<li><p><strong>Archimedes (circa 287–212 BCE):</strong> Pioneered the understanding of volume, surface area, and the principles of mechanics</p>
</li>
<li><p><strong>René Descartes (1596–1650):</strong> Developed Cartesian coordinates and analytical geometry</p>
</li>
<li><p><strong>Isaac Newton (1642–1727) &amp; Gottfried Wilhelm Leibniz (1646–1716):</strong> Newton’s laws of motion and gravitation, alongside Leibniz’s development of calculus, formed the basis of classical mechanics that Einstein sought to extend and modify in his theory of relativity.</p>
</li>
<li><p><strong>Leonhard Euler (1707–1783):</strong> Contributed to the development of differential equations, which are essential in the mathematical foundations of physics.</p>
</li>
<li><p><strong>Gaspard Monge (1746–1818):</strong> The father of differential geometry and pioneer in descriptive geometry</p>
</li>
<li><p><strong>Carl Friedrich Gauss (1777–1855):</strong> Made groundbreaking advances in geometry, including the concept of curved surfaces.</p>
</li>
<li><p><strong>Bernhard Riemann (1826–1866):</strong> Introduced Riemannian geometry, a branch of differential geometry.</p>
</li>
</ul>
<p>Once again, as Isaac Newton wrote, in a letter to Robert Hooke in 1675:</p>
<blockquote>
<p>If I have seen further, it is by standing on the shoulders of giants.</p>
</blockquote>
<p>Albert Einstein saw what no one else in his time saw, thanks to these great math giants and countless others.</p>
<h2 id="heading-the-biggest-paradox-of-math-discovered-by-kurt-godel">The Biggest Paradox of Math, Discovered by Kurt Gödel</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747518411126/df53f84c-f920-4b42-9081-5aeb1017f543.jpeg" alt="Kurt Gödel, one of the greatest math giants in history" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>The biggest paradox in math, in my opinion, is what Kurt Gödel discovered. His early 20th century research revealed a limitation within this cycle.</p>
<p>This paradox – that is, <a target="_blank" href="https://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorems">his incompleteness theorems</a> – shows that in any consistent formal system capable of expressing simple arithmetic, there will always be true mathematical statements that cannot be proven within the system itself.</p>
<p>This means that in ALL systems, there are limits to what you can actually prove as to what is true and false. For for mathematicians, this means that the tree will never be completed. There are truths that are beyond formal truths, and yet we still assume that they are true (albeit unproven).</p>
<p>This way, it proves that no matter how many mathematicians work in the field or how much AI is used to find new mathematics, there will always exist limitations. Some things are impossible to prove that are true, and we just know that they are due to approximation estimations and other non logical exact methods.</p>
<h2 id="heading-what-about-applied-math-and-engineering">What About Applied Math and Engineering?</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747518581076/606f3bce-d7db-4ac3-9322-833673a734b0.jpeg" alt="Photo by JESHOOTS.com: https://www.pexels.com/photo/person-holding-a-chalk-in-front-of-the-chalk-board-714699/" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>Applied math and engineering involves interpreting the same pure math ideas in real-world scenarios. Actually, in many cases, it is the combination of many math ideas. Let’s consider some examples:</p>
<p>Principal component analysis (PCA) is a widely used tool in data science. Yet, it is a mixture of linear algebra (in PCA, eigenvalues) with optimization (order eigenvalues that represent more data with less data) in order to make datasets shorter.</p>
<p>In machine learning, logistic regression is a mixture of calculus with statistics and probability.</p>
<p>In harmonic analysis, Laplace, Fourier, and Z-transforms are a way to see the same thing in a new domain to get new insights. In this case, integrals are used to make this mapping.</p>
<p>In deep learning, neural networks are just many matrices multiplying and updating themselves that adapt to model a dataset representing a system. This optimization of matrix values happens with activation functions, a gradient descent-based optimization method (tells how much values need to change), and backpropagation (applies those alterations to all matrix values).</p>
<p>I have actually written an article where I teach <a target="_blank" href="https://www.freecodecamp.org/news/activation-functions-in-neural-networks/">why activation functions are important</a> if you want to check it out.</p>
<p>But the best example of this fusion of math with engineering is in <a target="_blank" href="https://www.freecodecamp.org/news/basic-control-theory-with-python/">control theory</a>.</p>
<p>Control theory is the study of the architecture of systems. From trains to cars to airplanes, everything is based on control theory. It is everywhere in nearly all modern electronic devices. In electric circuits, control theory is also used heavily to guarantee circuit stability in the face of electric disturbances.</p>
<p>So as you can probably start to see, many of the tools we now have are just a mixture of many pure math ideas. Just many combinations and recipes of pure math ideas. In essence, applied math is the application of pure math as “ingredients“ in "recipes" to solve problems.</p>
<p>So, we’ve explored the structure and evolution of mathematics. Yet, it is important to see how these ideas can be applied in real life. Pure math makes the framework, and applied math applies that framework to solve problems. To understand this, we’ll examine two code examples that show how you can use math ideas as programming tools.</p>
<h2 id="heading-code-examples-analytical-and-numerical-approaches">Code Examples – Analytical and Numerical Approaches</h2>
<p>These code examples demonstrate a couple ways you can use Python to solve math equations.</p>
<p>In the first code example, we’ll solve the problem in the same way that kids in school solve math exercises: essentially, by hand with a pencil. Moving variables from left to right to find their values. In the second example, we’ll solve the problem using numerical analysis.</p>
<h3 id="heading-example-1-solve-a-problem-analytically">Example 1: Solve a Problem Analytically</h3>
<p>When we solve math problems analytically, like we did in school, we are manipulating symbols to get exact values. Often there symbols are x, y and z. In Python, we can do this using the SymPy library:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sympy <span class="hljs-keyword">import</span> symbols, Eq, solve

x, y = symbols(<span class="hljs-string">'x y'</span>)
eq1 = Eq(<span class="hljs-number">2</span>*x + <span class="hljs-number">3</span>*y, <span class="hljs-number">6</span>)
eq2 = Eq(-x + y, <span class="hljs-number">1</span>)

solution = solve((eq1, eq2), (x, y))
print(solution)
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747160359386/7a21cddc-f4ba-4f9f-afa0-d1cc11fb27d6.png" alt="7a21cddc-f4ba-4f9f-afa0-d1cc11fb27d6" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>Essentially, we are finding x and y based on this equation:</p>
<p>$$\begin{align*} 2x + 3y &amp;= 6 \\ -x + y &amp;= 1 \end{align*}$$</p><p>Which gives us the following result:</p>
<pre><code class="lang-python">{x: <span class="hljs-number">3</span>/<span class="hljs-number">5</span>, y: <span class="hljs-number">8</span>/<span class="hljs-number">5</span>}
</code></pre>
<p>Or:</p>
<ul>
<li><p>x= 0.6</p>
</li>
<li><p>y = 1.6</p>
</li>
</ul>
<p>When we say that we’re solving this analytically, it means that we’re finding an exact mathematical solution using formulas or equations.</p>
<p>But many times, problems are harder and can be solved by adding symbols to the right or left of the equation.</p>
<p>Sometimes, there can be so many symbols and transformed versions of them, with things like derivatives and integrals, that it can become very hard to manage and takes a lot of time.</p>
<p>For this reason, there is an area of mathematics devoted to finding approximations of already created mathematical formulas called numerical analysis. It makes it faster to solve these problems. And this is the method we will explore next.</p>
<h3 id="heading-example-2-solve-numerically-approximation">Example 2: Solve Numerically (Approximation)</h3>
<p>We’ll now use SciPy to solve the same system with numerical methods:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> scipy.linalg <span class="hljs-keyword">import</span> solve

A = np.array([[<span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>],
              [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">-2</span>],
              [<span class="hljs-number">4</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>],
              [<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">-2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>],
              [<span class="hljs-number">2</span>, <span class="hljs-number">-3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]])

b = np.array([<span class="hljs-number">12</span>, <span class="hljs-number">5</span>, <span class="hljs-number">7</span>, <span class="hljs-number">9</span>, <span class="hljs-number">10</span>])

solution = solve(A, b)

print(solution)
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747160347486/d1f17aa6-b288-4e41-9be7-0810c45e778c.png" alt="d1f17aa6-b288-4e41-9be7-0810c45e778c" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>In this code example, this line of code:</p>
<pre><code class="lang-python">solution = solve(A, b)
</code></pre>
<p>Uses the <a target="_blank" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.solve.html">solve</a> method from the <a target="_blank" href="https://scipy.org/">SciPy</a> Python library:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> scipy.linalg <span class="hljs-keyword">import</span> solve
</code></pre>
<p>It’s a method that helps you find the values of x in an equation A⋅x=b, where a is a square grid of numbers and b is a list of numbers. Which gives us the following:</p>
<pre><code class="lang-python">[ <span class="hljs-number">1.35022026</span> <span class="hljs-number">-0.79955947</span> <span class="hljs-number">-1.17180617</span>  <span class="hljs-number">3.14317181</span> <span class="hljs-number">-0.83920705</span>]
</code></pre>
<p>Now imagine, in this simple case, that a matrix like A could represent the <strong>traffic flow</strong> between cities or intersections, and b could represent the <strong>traffic entering or leaving</strong> each city.</p>
<p>By solving the system, it could help us determine the distribution of traffic between cities to meet desired traffic conditions.</p>
<p>Of course, these types of problems are far more complex in real life. But to understand and solve the big problems, you need to first understand the smaller problems.</p>
<p>And by the way, a system of equations is the same thing as a matrix. We just represent systems of equations as matrices to make the findings of properties and clarity easier to understand.</p>
<p>The thing is that by using matrices, it is easier to make calculations and to perform linear algebra math to check for characteristics of the matrix and understand it better.</p>
<p>In essence, a matrix represents a system of equations. Also, systems of equations can represent real life phenomena like the economy of a country or the weather.</p>
<p>If you want to know more, I wrote an <a target="_blank" href="https://www.freecodecamp.org/news/numerical-analysis-explained-how-to-apply-math-with-python/">entire article on numerical analysis</a> that you can check out.</p>
<h2 id="heading-the-impact-of-a-grand-unified-theory-of-mathematics">The Impact of a Grand Unified Theory of Mathematics</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747518681068/54a9556c-2a79-441c-a6d6-27ff38e1f4ff.jpeg" alt="Photo by Porapak Apichodilok: https://www.pexels.com/photo/person-holding-world-globe-facing-mountain-346885/" class="image--center mx-auto" width="600" height="400" loading="lazy"></p>
<p>Despite the biggest paradox in mathematics, what would happen with a <a target="_blank" href="https://www.scientificamerican.com/article/the-evolving-quest-for-a-grand-unified-theory-of-mathematics/">Grand Unified Theory of Mathematics</a>?</p>
<p>Remember that such a theory tells us that there are things that are true that are impossible to formally prove, and we need to just accept it. But even with this assumption, it is still possible to unify all math.</p>
<p>This is what <a target="_blank" href="https://en.wikipedia.org/wiki/Langlands_program">the Langland's program</a> is trying to solve. A kind of attempt to interconnect the largest parts of the big tree of math to uncover new patterns in math.</p>
<p>With a Grand Unified Theory of Mathematics, we would be able to understand how every branch of the tree connects with the others and all the relationships between them.</p>
<h3 id="heading-what-is-the-value-of-this-big-unification-for-society">What is the value of this big unification for society?</h3>
<p>By studying history, we can find patterns. The unification of various fields has created many massive impacts on society, such as:</p>
<ul>
<li><p>In the 19th century, James Clerk Maxwell united the fields of <em>electricity</em> and <em>magnetism</em> with his famous Maxwell equations. This allowed the creation of radios and electric grids around the globe. In turn, it served as a foundation for all technological progress in the 20th and 21th century.</p>
</li>
<li><p>In the 20th century, the unification of <em>algebra</em> with <em>logic</em> led to the rise of digital systems. In turn, digital systems gave the rise of processors and the evolution of computers to the modern laptop.</p>
</li>
<li><p>Also in the 20th century, the unification of <em>probability</em> and <em>communication</em> led to information theory. This became the foundation for the internet. This unification was carried out by a great mathematician called Clause Shannon.</p>
</li>
</ul>
<p>In the end, a Grand Unified Theory of Mathematics could be one of the biggest achievements in modern society.</p>
<p>It could lead to new discoveries in physics, such as in string theory or quantum gravity, where deep mathematical structures are needed to create new physics. In AI, it could help unify all machine learning models in a common architecture. This would help accelerate the development of new AI models. It could also open the door to new cryptographic methods and material science advances, revealing, with math, the deep patterns still not found in these fields.</p>
<p>Just as uniting electricity and magnetism led to modern technology, a unified math framework would lead to a wave of innovation.</p>
<h2 id="heading-a-final-lesson-from-history">A Final Lesson From History</h2>
<p>From Greek geometry to AI, math has grown like a tree over centuries. By understanding its structure, it is possible to see its role in finding the patterns of our universe. I hope I was able to make you see math in this way.</p>
<p>In addition, we can conclude that the unification of scientific fields makes the foundations for the creation of new innovations to help society go forward. Many profound societal transformations only came to be thanks to abstract math ideas. When these are shared and refined, they become the hidden architecture of progress in society. Innovation begins when disconnected ideas are united, well-linked, and widely shared.</p>
<p>Find the full code <a target="_blank" href="https://github.com/tiagomonteiro0715/freecodecamp-my-articles-source-code">here</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Write Math Equations in Google Docs ]]>
                </title>
                <description>
                    <![CDATA[ Math equations are a critical part of academic papers, research reports, and technical documentation. While LaTeX is widely used for professional typesetting, Google Docs offers a robust set of features for inserting and formatting math equations and... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/write-math-equations-in-google-docs/</link>
                <guid isPermaLink="false">68275cf15ed8a1db0cf8769b</guid>
                
                    <category>
                        <![CDATA[ Google Docs ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ chatgpt ]]>
                    </category>
                
                    <category>
                        <![CDATA[ chatgptguide ]]>
                    </category>
                
                    <category>
                        <![CDATA[ MathJax ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Vikram Aruchamy ]]>
                </dc:creator>
                <pubDate>Fri, 16 May 2025 15:42:41 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1747410133881/93c8cf6e-6cbb-4890-8023-fcc2fdaa0fa2.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Math equations are a critical part of academic papers, research reports, and technical documentation. While LaTeX is widely used for professional typesetting, Google Docs offers a robust set of features for inserting and formatting math equations and also supports LaTeX-style input.</p>
<p>Whether you're a student submitting a math assignment or a professional documenting formulas, Google Docs provides multiple ways to insert and format equations efficiently.</p>
<p>In this article, you'll learn how to <a target="_blank" href="https://support.google.com/docs/answer/160749">write math equations in Google Docs</a> using different methods, including using Google Docs’ built-in equation editor and typing LaTeX-style commands directly, inserting complex equations with the help of the Auto-LaTeX add-on, and copying math equations from <a target="_blank" href="https://chromewebstore.google.com/detail/chatgpt-to-google-docs-or/oibghjgooccojibfacdonaoipegckdeg">ChatGPT to Google Docs</a> without losing formatting by using the ChatGPT to Google Docs or PDF Chrome extension.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-how-to-write-equations-using-the-built-in-equation-editor">How to Write Equations Using the Built-in Equation Editor</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-write-equations-using-latex-commands">How to Write Equations Using LaTeX Commands</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-use-auto-latex-add-on-for-writing-advanced-math-equations">How to Use Auto Latex Add-on for Writing Advanced Math Equations</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-copy-math-equations-from-chatgpt-to-google-docs">How to Copy Math Equations from ChatGPT to Google Docs</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-watch-how-to-write-equations-in-google-docs">Watch: How to Write Equations in Google Docs</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-tips-for-formatting-math-equations-in-google-docs">Tips for Formatting Math Equations in Google Docs</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-how-to-write-equations-using-the-built-in-equation-editor"><strong>How to Write Equations Using the Built-in Equation Editor</strong></h2>
<p>Google Docs has a built-in equation editor that makes it easy to insert mathematical symbols and expressions.</p>
<p>To insert an equation editor box:</p>
<ol>
<li><p>Open your Google Docs document.</p>
</li>
<li><p>Go to the top menu and click <em>Insert</em> → <em>Equation</em>.</p>
</li>
<li><p>An equation editor will appear, and a new toolbar will show up with common math symbols like fractions, exponents, Greek letters, and more.</p>
</li>
</ol>
<p>Alternatively, you can use the following keyboard shortcuts to insert an equation editor box. </p>
<ul>
<li><p><strong>Windows/Linux:</strong> <code>Alt</code> + <code>I</code>, then <code>E</code></p>
</li>
<li><p><strong>Mac:</strong> <code>Control</code> + <code>Option</code> + <code>I</code>, then <code>E</code></p>
</li>
</ul>
<p>This shortcut quickly opens the equation editor without clicking through menus.</p>
<p><strong>Toolbar Symbols:</strong></p>
<p>Once the toolbar appears, you’ll find buttons for:</p>
<ul>
<li><p>Greek letters</p>
</li>
<li><p>Miscellaneous operations</p>
</li>
<li><p>Relations</p>
</li>
<li><p>Math operations</p>
</li>
<li><p>Arrows</p>
</li>
</ul>
<p>The equation editor box and a toolbar look like the following:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747198733591/9defa152-16a5-42a1-b098-228a9d1f7f79.png" alt="Google Docs Equation Editor and Toolbar" class="image--center mx-auto" width="1340" height="450" loading="lazy"></p>
<p>Now let’s learn how to write equations using the equation editor with a practical example.</p>
<h3 id="heading-example-typing-the-quadratic-formula">Example: Typing the Quadratic Formula</h3>
<p>Follow these steps to insert the following quadratic formula in Google Docs:</p>
<p>$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$</p><ol>
<li><p>Go to <em>Insert</em> → <em>Equation</em> to insert an equation editor and enable the equation toolbar.</p>
</li>
<li><p>Type <code>x=</code></p>
</li>
<li><p>Click the Math Operations dropdown (the one with templates like square roots, brackets), then select the fraction template. This inserts a placeholder with two parts: a numerator and a denominator.</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747199319708/142157c9-f997-494a-be4a-efb619b7c12d.png" alt="Screenshot of Google Docs displaying a feature to insert math equations. A dropdown menu shows various mathematical symbols like fractions, square roots, and integrals. " class="image--center mx-auto" width="721" height="418" loading="lazy"></p>
</li>
<li><p>Click inside the numerator field. Begin by typing <code>-b</code>.</p>
</li>
<li><p>Now insert the ± (plus-minus) symbol. To do this:</p>
<ul>
<li><p>Click the Miscellaneous Operations dropdown</p>
</li>
<li><p>Select the ± symbol from the list.<br>  Your numerator should now show: <code>-b ±</code> as in the following image:</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747199567908/be5daee7-2ed2-4c86-be16-d90e033a05be.png" alt="Screenshot of Google Docs showing a math equation being edited with the equation toolbar open. Various symbols are visible on the toolbar, and part of a quadratic formula is visible in the document area." class="image--center mx-auto" width="766" height="362" loading="lazy"></p>
</li>
</ul>
</li>
<li><p>After the ± symbol, insert a square root:</p>
<ul>
<li><p>Go back to the Math Operations dropdown and select the square root template.</p>
</li>
<li><p>Inside the root, type <code>b^2 - 4ac</code>.</p>
<ul>
<li><p>Use <code>^</code> to enter exponents. For example, <code>b^2</code> will be rendered as <em>b²</em>.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747199681755/cc2084c2-5856-4999-9905-d825b5c276d9.png" alt="Screenshot of Google Docs showing a math equation being inserted. A dropdown menu displays mathematical symbols like fractions, square roots, and integrals. A formula for the quadratic equation is in the document area on the right." class="image--center mx-auto" width="775" height="393" loading="lazy"></p>
</li>
</ul>
</li>
</ul>
</li>
<li><p>Move to the denominator field and type <code>2a</code>.</p>
</li>
</ol>
<p>Now your full equation should appear as:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747199739733/2c1ef4f8-9730-491f-9120-5ba6daee41c3.png" alt="Screenshot of Google Docs showing an open document. It has menu options at the top and a formula for the quadratic equation displayed on the page." class="image--center mx-auto" width="805" height="352" loading="lazy"></p>
<p>The equation will be properly formatted using Google Docs’ equation rendering, making it easy to read and mathematically accurate. You can continue typing more text below or beside the equation as needed – it behaves like any other element in your document.</p>
<p>This approach is useful for inserting neatly formatted equations without relying on add-ons or external tools. It’s especially helpful for students, teachers, and professionals preparing technical documents directly in Google Docs.</p>
<h2 id="heading-how-to-write-equations-using-latex-commands">How to Write Equations Using LaTeX Commands</h2>
<p>If you're familiar with <a target="_blank" href="https://www.latex-project.org/"><strong>LaTeX</strong></a>, you can take advantage of Google Docs’ support for a subset of LaTeX-style commands inside the built-in equation editor. This can greatly speed up the process of entering complex mathematical expressions, especially if you're already comfortable with LaTeX syntax.</p>
<h3 id="heading-how-to-use-latex-commands-in-google-docs">How to Use LaTeX Commands in Google Docs</h3>
<ol>
<li><p>Open your Google Docs document.</p>
</li>
<li><p>Go to Insert → Equation to activate the equation toolbar and equation input field.</p>
</li>
<li><p>Click inside the equation box. Instead of using the toolbar buttons, type LaTeX-style commands directly.</p>
</li>
<li><p>As you type, Google Docs will automatically render the commands into formatted math once you press space or enter after each command or expression.</p>
</li>
</ol>
<h4 id="heading-commonly-supported-latex-commands-in-google-docs">Commonly Supported LaTeX Commands in Google Docs:</h4>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Instruction</strong></td><td><strong>Result</strong></td></tr>
</thead>
<tbody>
<tr>
<td>To insert a fraction</td><td><code>\frac{a}{b}</code> → 𝑎⁄𝑏</td></tr>
<tr>
<td>To insert a square root</td><td><code>\sqrt{x}</code> → √𝑥</td></tr>
<tr>
<td>To insert Greek letters like α, β</td><td><code>\alpha, \beta</code> → α, β</td></tr>
<tr>
<td>To insert an integral with limits</td><td><code>\int_a^b f(x)\,dx</code> → ∫ᵃᵇ 𝑓(𝑥) 𝑑𝑥</td></tr>
<tr>
<td>To insert x superscript 2</td><td><code>x^2</code> → 𝑥²</td></tr>
<tr>
<td>To insert x subscript 1</td><td><code>x_1</code> → 𝑥₁</td></tr>
</tbody>
</table>
</div><p>Type these commands in the equation box, and when you press space or enter, they will be converted to properly formatted mathematical notation.</p>
<h4 id="heading-example-typing-the-quadratic-formula-using-latex-commands">Example: Typing the Quadratic Formula Using LaTeX Commands</h4>
<p>Let’s walk through how to enter the following quadratic formula using LaTeX-style commands:</p>
<p>$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$</p><p><strong>Steps:</strong></p>
<ol>
<li><p>Insert the equation box: Go to Insert → Equation.</p>
</li>
<li><p>In the equation input area, type the following:</p>
</li>
</ol>
<pre><code class="lang-javascript">x = \frac{-b \pm \sqrt{b^<span class="hljs-number">2</span> - <span class="hljs-number">4</span>ac}}{<span class="hljs-number">2</span>a}
</code></pre>
<ul>
<li><p><code>\frac</code> creates a fraction.</p>
</li>
<li><p><code>-b</code> is the numerator’s first term.</p>
</li>
<li><p><code>\pm</code> inserts the plus-minus symbol.</p>
</li>
<li><p><code>\sqrt</code> creates a square root.</p>
</li>
<li><p><code>b^2</code> formats <em>b squared</em>.</p>
</li>
<li><p><code>- 4ac</code> is written normally inside the square root.</p>
</li>
<li><p><code>2a</code> is the denominator.</p>
</li>
</ul>
<ol start="3">
<li>As you type, press space or enter after each LaTeX command. Google Docs will automatically convert the code into properly formatted math notation.</li>
</ol>
<p>After rendering, the equation will appear as:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747199739733/2c1ef4f8-9730-491f-9120-5ba6daee41c3.png" alt="A Google Docs interface showing the quadratic formula being entered as a math equation" width="805" height="352" loading="lazy"></p>
<p>This method is ideal for users who prefer keyboard-based input over clicking toolbar icons. It also allows you to enter complex expressions faster and more accurately, especially if you're familiar with standard LaTeX syntax.</p>
<h4 id="heading-notes">Notes:</h4>
<ul>
<li><p>Not all LaTeX features are supported in Google Docs. The supported commands are limited to basic math formatting, Greek letters, and common symbols.</p>
</li>
<li><p>Make sure to press <strong>space</strong> after each LaTeX command so that Docs knows to render it.</p>
</li>
</ul>
<h2 id="heading-how-to-use-auto-latex-add-on-for-writing-advanced-math-equations">How to Use Auto Latex Add-on for Writing Advanced Math Equations</h2>
<p>When generating mathematical content using tools like ChatGPT, you'll notice that equations are <strong>rendered visually on the webpage</strong>, but behind the scenes they’re created using <a target="_blank" href="https://www.latex-project.org/">LaTeX</a> code. So when you copy content from ChatGPT into Google Docs, the equations come through as raw LaTeX code rather than rendered math expressions.</p>
<p>For example, a quadratic formula provided by ChatGPT might look like this when pasted into your document:</p>
<pre><code class="lang-javascript">x = \frac{-b \pm \sqrt{b^<span class="hljs-number">2</span> - <span class="hljs-number">4</span>ac}}{<span class="hljs-number">2</span>a}
</code></pre>
<p>While this format is ideal for precision, Google Docs doesn’t support LaTeX rendering by default.</p>
<p>This is where the <strong>Auto-LaTeX Equations</strong> add-on becomes essential, especially if you're moving content from <a target="_blank" href="https://www.docstomarkdown.pro/chatgpt-to-google-docs/">ChatGPT to Google Docs</a>. It’s also incredibly useful when importing LaTeX-based documents into Google Docs, such as content originally written in <a target="_blank" href="https://overleaf.com/">Overleaf</a> or other LaTeX editors.</p>
<p>Instead of manually reformatting equations, the add-on automatically renders LaTeX code into properly formatted math equations, preserving the typesetting and structure you’d expect from a LaTeX environment.</p>
<h3 id="heading-what-is-auto-latex-equations">What is Auto-LaTeX Equations?</h3>
<p><a target="_blank" href="https://workspace.google.com/marketplace/app/autolatex_equations/850293439076"><strong>Auto-LaTeX Equations</strong></a> is a free and open-source Google Docs add-on that scans your document for LaTeX expressions and converts them into a properly formatted equations.</p>
<p>It recognizes LaTeX code wrapped in these delimiters:</p>
<ul>
<li><p>Inline: <code>$$ ... $$</code></p>
</li>
<li><p>Display: <code>\[ ... \]</code></p>
</li>
</ul>
<p>Once detected, it renders the equations seamlessly within your document, eliminating the need to retype or manually format them.</p>
<ol>
<li><p>Paste your LaTeX expression into the Google Docs document. Make sure the expression is enclosed using one of the supported delimiters:</p>
<p> <code>$$ ... $$ or \[ ... \]</code></p>
</li>
<li><p>Open the add-on sidebar by clicking Extensions <strong>→</strong> Auto-LaTeX Equations <strong>→</strong> Start.</p>
</li>
<li><p>Once the sidebar opens, you’ll see a dropdown labeled “Delimiters” and a button called “Render Equations.”</p>
</li>
<li><p>Select the delimiter you used when enclosing your LaTeX equations – for example, <code>$$</code> or <code>\[ \]</code>.</p>
</li>
<li><p>Click the “Render Equations” button.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747203253430/e96fe9ac-ccba-4ed5-ab10-1e759d737d7d.png" alt="Auto Latex Equations add-on sidebar" class="image--center mx-auto" width="297" height="387" loading="lazy"></p>
<p>The add-on will automatically scan your document and convert all valid LaTeX expressions into properly formatted equation images.</p>
<p>This step-by-step process allows you to take any LaTeX-based math copied from ChatGPT and render it cleanly within Google Docs – ready for export to Word or PDF.</p>
<h3 id="heading-example-converting-a-latex-coded-equation-to-rendered-math-equations">Example: Converting a LaTeX coded Equation to Rendered Math Equations</h3>
<p>Paste the following equation into Google Docs:</p>
<p><code>$$ x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} $$</code></p>
<p>To convert it:</p>
<ol>
<li><p>Go to Extensions → Auto-LaTeX Equations → Start.</p>
</li>
<li><p>Select the Delimitor as <code>$$</code> ..<code>$$</code> and click on the Render Equations button. The equation will be rendered and look as follows:</p>
</li>
</ol>
<p>$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$</p><h4 id="heading-how-to-install-auto-latex-equations">How to Install Auto-LaTeX Equations</h4>
<ol>
<li><p>In Google Docs, click Extensions → Add-ons → Get add-ons.</p>
</li>
<li><p>Search for Auto-LaTeX Equations.</p>
</li>
<li><p>Click Install and follow the prompts.</p>
</li>
<li><p>After installation, access it from Extensions → Auto-LaTeX Equations.</p>
</li>
</ol>
<h2 id="heading-how-to-copy-math-equations-from-chatgpt-to-google-docs">How to Copy Math Equations from ChatGPT to Google Docs</h2>
<p>To easily transfer math equations and the surrounding content from ChatGPT into Google Docs without losing formatting, use the free <a target="_blank" href="https://chromewebstore.google.com/detail/chatgpt-to-google-docs-or/oibghjgooccojibfacdonaoipegckdeg"><strong>ChatGPT to Google Docs or PDF</strong></a> Chrome extension.</p>
<p>This extension allows you to:</p>
<ul>
<li><p>Export a single response (with equations and tables) into Google Docs while preserving formatting</p>
</li>
<li><p>Export an entire conversation, including math, code, and text, into a clean, one organized Google Docs, no need to export responses separately and <a target="_blank" href="https://www.freecodecamp.org/news/merge-multiple-google-docs-with-apps-script-or-google-docs-api/">merge multiple Google Docs into one</a> later</p>
</li>
<li><p>Save ChatGPT canvas content as a Google Docs or PDF</p>
</li>
<li><p>Export ChatGPT deep research documents directly into Google Docs</p>
</li>
<li><p>Export <a target="_blank" href="https://www.docstomarkdown.pro/chatgpt-to-pdf/">ChatGPT content directly into PDF format</a> when no further edits are necessary, eliminating the need to first export to Google Docs and then convert <a target="_blank" href="https://workspace.google.com/marketplace/app/docs_to_pdf_pro/302636103705">Google Docs to PDF</a></p>
</li>
</ul>
<p>It’s especially useful for students, researchers, and professionals who want to keep their AI-generated math, notes, and research well-organized in Google Docs or PDF format with minimal effort.</p>
<h2 id="heading-watch-how-to-write-equations-in-google-docs">Watch: How to Write Equations in Google Docs</h2>
<p>If you prefer visual learning, here’s a helpful video walkthrough that demonstrates all the methods discussed above – using the built-in equation editor, LaTeX-like commands, and the Auto-LaTeX Equations add-on.</p>
<p>This step-by-step tutorial covers:</p>
<ul>
<li><p>Opening and using the built-in equation toolbar</p>
</li>
<li><p>Typing LaTeX-style commands directly in the equation editor</p>
</li>
<li><p>Converting AI-generated LaTeX (e.g., from ChatGPT) into clean equations</p>
</li>
</ul>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/Y_2q45Qscp0" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<h2 id="heading-tips-for-formatting-math-equations-in-google-docs">Tips for Formatting Math Equations in Google Docs</h2>
<p><strong>Use inline equations when:</strong></p>
<ul>
<li><p>Inserting short expressions like <code>x²</code>, <code>a/b</code>, or single variables</p>
</li>
<li><p>Including math within a sentence to maintain the flow of text</p>
</li>
</ul>
<p><strong>Use block equations when:</strong></p>
<ul>
<li><p>Writing complex or multi-line formulas (e.g., the quadratic formula)</p>
</li>
<li><p>You want the equation to be clearly separated from the surrounding text for readability</p>
</li>
</ul>
<p><strong>Wrapping tips for rendered equations:</strong></p>
<ul>
<li><p>Rendered equations are treated as images in Google Docs, which may disrupt the document layout if not positioned correctly</p>
</li>
<li><p>To fix this:</p>
<ul>
<li><p>Click the equation image</p>
</li>
<li><p>Choose from:</p>
<ul>
<li><p><strong>In line</strong> – aligns the equation with surrounding text (best for inline use)</p>
</li>
<li><p><strong>Wrap text</strong> – wraps paragraph text around the equation image</p>
</li>
<li><p><strong>Break text</strong> – places the equation on its own line, isolating it</p>
</li>
</ul>
</li>
<li><p>Use the margin handles or spacing options to fine-tune the layout and prevent overlap or crowding</p>
</li>
</ul>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Google Docs offers several flexible ways to write and manage math equations:</p>
<ul>
<li><p><strong>Use the built-in equation editor</strong> for basic symbols, fractions, exponents, and common operations. It’s easy to access and great for straightforward math tasks without needing special syntax.</p>
</li>
<li><p><strong>Try LaTeX-like commands</strong> inside the equation editor for faster input. You can type commands like <code>\frac</code>, <code>\sqrt</code>, or <code>\alpha</code> to quickly insert structured equations without navigating menus.</p>
</li>
<li><p><strong>Install add-ons like Auto-LaTeX Equations</strong> for advanced LaTeX rendering. This is especially useful if you're copying equations from Overleaf, ChatGPT, or LaTeX documents into Google Docs, as it preserves formatting and converts code into clean equation images.</p>
</li>
<li><p><strong>Use external tools when copying from other formats</strong>, like the <em>ChatGPT to Google Docs or PDF</em> Chrome extension, which helps retain equation formatting when moving content from ChatGPT or other platforms.</p>
</li>
</ul>
<p>Whether you’re completing math homework, preparing teaching materials, or writing a research paper, Google Docs, combined with these tools, gives you everything you need to create clear, professional-looking documents with math content.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn Calculus by Coding in Python ]]>
                </title>
                <description>
                    <![CDATA[ Calculus is one of the cornerstones of higher mathematics and a powerful tool for understanding change, motion, and growth across countless disciplines. But for many students, Calculus can seem intimidating or abstract. What if you could learn it ste... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-college-calculus-and-implement-with-python/</link>
                <guid isPermaLink="false">6810eebd43aa3d16a14700eb</guid>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Tue, 29 Apr 2025 15:22:37 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745940136487/3bf6eb1d-4ffa-4d41-9ac7-89a9f3836915.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Calculus is one of the cornerstones of higher mathematics and a powerful tool for understanding change, motion, and growth across countless disciplines. But for many students, Calculus can seem intimidating or abstract. What if you could learn it step by step from a seasoned university professor, and simultaneously see how each concept works in code? That’s exactly what this new course offers: a practical, intuitive, and coding-friendly approach to mastering Calculus.</p>
<p>We just published a course on the <a target="_blank" href="http://freeCodeCamp.org">freeCodeCamp.org</a> YouTube channel that will teach you all about Calculus through the lens of Python programming. Taught by experienced mathematics professor Ed Pratowski, this course walks you through essential topics in college-level Calculus while showing you how to implement these concepts using Python.</p>
<p>The course begins with foundational concepts like limits and the idea of a "hole in the graph" before moving into derivative rules, slope interpretation, and real-world applications like financial modeling and projectile motion. You'll explore important theorems like Rolle's and the Mean Value Theorem, as well as integral calculus topics like Riemann sums, the Fundamental Theorem of Calculus, and calculating volume using solids of revolution. You'll also learn how to apply symbolic math libraries like SymPy for graphing and computation.</p>
<p>One of the unique features of this course is its consistent use of Python to illustrate and reinforce each Calculus concept. By coding derivatives, plotting graphs, and computing integrals programmatically, you'll not only deepen your mathematical understanding but also gain practical skills in mathematical programming. This is an invaluable asset for data science, engineering, and technical careers.</p>
<p>Here is the full list of topics covered.</p>
<ul>
<li><p>Intro: Calculus with Python</p>
</li>
<li><p>Limits: Hole in the Graph</p>
</li>
<li><p>Limits: Asymptotes</p>
</li>
<li><p>Limits: Graphing</p>
</li>
<li><p>Limits and Slope</p>
</li>
<li><p>Slope and the Derivative</p>
</li>
<li><p>Derivatives and Calculus</p>
</li>
<li><p>Chain Rule</p>
</li>
<li><p>Product Rule</p>
</li>
<li><p>Implicit Differentiation</p>
</li>
<li><p>Multiple Derivative Steps</p>
</li>
<li><p>Derivative Example</p>
</li>
<li><p>Financial Applications</p>
</li>
<li><p>Projectile Motion</p>
</li>
<li><p>Derivatives and Differentials</p>
</li>
<li><p>Tangent Lines</p>
</li>
<li><p>Parametric Equations</p>
</li>
<li><p>Related Rates: Ladder Sliding</p>
</li>
<li><p>Related Rates: Balloon Volume</p>
</li>
<li><p>Mean Value Theorem</p>
</li>
<li><p>Rolles Theorem</p>
</li>
<li><p>Riemann Sums: Area Under a Curve</p>
</li>
<li><p>Summation and the Integral</p>
</li>
<li><p>Fundamental Theorem of Calculus</p>
</li>
<li><p>Area Above and Below the Axis</p>
</li>
<li><p>Area Between Curves</p>
</li>
<li><p>Volume Revolved Around X</p>
</li>
<li><p>Volume of a Hollow Shape</p>
</li>
<li><p>Volume Revolved Around Y</p>
</li>
<li><p>Center of Mass</p>
</li>
<li><p>The Normal Curve</p>
</li>
<li><p>Sympy Graphing</p>
</li>
<li><p>Arc Length</p>
</li>
<li><p>Surface Area</p>
</li>
<li><p>Integral Formulas</p>
</li>
</ul>
<p>Ready to bring math to life with code? Check out this comprehensive Calculus with Python course on the <a target="_blank" href="https://youtu.be/VDFRpjQVaME">freeCodeCamp.org YouTube channel</a>.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/VDFRpjQVaME" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Run LaTeX Projects Locally (for Free) On Windows ]]>
                </title>
                <description>
                    <![CDATA[ LaTeX is a high-quality typesetting system that is widely used in technical, academic, and scientific writing. It’s very popular in academia, especially in fields like mathematics, physics, computer science, and engineering. LaTeX is not a word proce... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-run-latex-projects-locally-for-free-on-windows/</link>
                <guid isPermaLink="false">67bde54468de46f577ed25f8</guid>
                
                    <category>
                        <![CDATA[ latex ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Windows ]]>
                    </category>
                
                    <category>
                        <![CDATA[ research ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Md. Fahim Bin Amin ]]>
                </dc:creator>
                <pubDate>Tue, 25 Feb 2025 15:44:04 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1740494599916/ce7cfadb-985c-4245-9cc8-1ccba483ba69.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>LaTeX is a high-quality typesetting system that is widely used in technical, academic, and scientific writing. It’s very popular in academia, especially in fields like mathematics, physics, computer science, and engineering.</p>
<p>LaTeX is not a word processor like Microsoft Word – rather, it’s a document preparation system that allows you to focus on the content of your writing while it handles the formatting. If you use LaTeX to write your formal documents (like a CV, résumé, or research paper), then you don’t need to worry about the formatting and structure, as everything can be done using LaTeX scripts.</p>
<p>If you use LaTeX to write your academic or research papers, you might be familiar with website-based applications like <a target="_blank" href="https://www.overleaf.com/">Overleaf</a>. Overleaf is a website that allows anyone to read, write, and compile LaTeX scripts online.</p>
<p>These sites are okay for small tasks or compilations, or if you need only a little bit of free collaboration. But if you need to work on bigger projects or need to conduct many collaborative tasks, then the free tier may be insufficient. And in my opinion, the paid subscription costs too much.</p>
<p>But don’t worry: running LaTeX locally may be the perfect solution for you. I know this because I also faced a similar situation, and this simply changed my life! I also keep all of the tracks in Git (GitHub, GitLab, and so on) along with unlimited collaboration opportunities and compilation. And the great thing is, all of these are completely free as it’s all happening on my local machine.</p>
<p>So in this article, I am going to discuss the methods in detail. I have also created an in-depth video for you to understand how this works.</p>
<h3 id="heading-video-tutorial">Video Tutorial</h3>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/A45lWrndVHA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<h2 id="heading-resources-youll-need">Resources You’ll Need:</h2>
<h3 id="heading-1-github-repository">1. GitHub Repository</h3>
<p>This entire guide is available in one of my GitHub projects named <a target="_blank" href="https://github.com/FahimFBA/Install-LaTeX">Install-LaTeX</a>. The live website is available <a target="_blank" href="https://fahimfba.github.io/install-latex/">here</a> (<a target="_blank" href="https://fahimfba.github.io/Install-LaTeX/">fahimfba.github.io/Install-LaTeX</a>) as well. I would highly appreciate it if you star (⭐) the repository. Also, you can create issues <a target="_blank" href="https://github.com/FahimFBA/Install-LaTeX/issues">there</a> if you face any problems. Any kind of good contribution is also welcome here.</p>
<h3 id="heading-2-operating-system">2. Operating System</h3>
<p>You can install LaTeX on any major operating system (Windows, MacOS, and Linux-based OSes). But in this article, I am only going to talk about the Windows operating system.</p>
<p>Here, I’m using the latest Windows 11 operating system, but the same procedure should be applicable to all of the Windows-based operating systems that are going to come out in the future. Windows 10 should also be okay too.</p>
<h3 id="heading-3-editor">3. Editor</h3>
<p>I am going to use the popular <a target="_blank" href="https://code.visualstudio.com/">Visual Studio Code</a> as my editor. It is a 100% free and robust editor that’s very popular among devs all over the world. If you don’t already have it, go ahead and install it before proceeding further.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972339481/729ecab1-b87e-43d6-baf5-dbda170bcefc.png" alt="VS Code" class="image--center mx-auto" width="1761" height="921" loading="lazy"></p>
<h3 id="heading-4-latex-compileride">4. LaTeX Compiler/IDE</h3>
<p>To work on LaTeX files, you’ll need a specific compiler. I am going to use <a target="_blank" href="https://miktex.org/">MikTeX</a>. There are other tools out there, but this is the best tool right now (according to me!). It is completely free and supports all major operating systems as well. It also has a built-in IDE, but we are going to use VS Code as our main editor.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972305065/8119b362-3c95-42a0-9458-be211d2ead35.png" alt="MiKTeX" class="image--center mx-auto" width="1631" height="905" loading="lazy"></p>
<p>Download the Windows executable file from the Download section.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972408494/71b39032-3adc-426e-8bd9-3a18dc454cf5.png" alt="Download MiKTeX" class="image--center mx-auto" width="1312" height="838" loading="lazy"></p>
<p>After the download is finished, install the executable. At the end of the installation, keep the tick in “Check for updates now”.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972492255/a59f7000-8137-46c5-89f1-4f7151a751b6.png" alt="Check for update" class="image--center mx-auto" width="524" height="403" loading="lazy"></p>
<p>You will find the MikTeX console in your taskbar. Open that.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972541856/e7bad89a-4920-4175-a361-ba8fb51f6b20.png" alt="MiKTeX console" class="image--center mx-auto" width="856" height="669" loading="lazy"></p>
<p>Go to the “Updates” tab and click “Update now”. It will install all of those packages.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972581283/6d585388-0218-4792-b78e-798c75dee6a6.png" alt="6d585388-0218-4792-b78e-798c75dee6a6" class="image--center mx-auto" width="821" height="645" loading="lazy"></p>
<p>At the end, it will prompt you to close the console. Click “OK”. Open MiKTeX again.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972610846/926b5aba-0a3a-4c80-a103-fef5a5aafc38.png" alt="926b5aba-0a3a-4c80-a103-fef5a5aafc38" class="image--center mx-auto" width="841" height="663" loading="lazy"></p>
<p>That’s it for this tool.</p>
<h3 id="heading-5-perl">5. Perl</h3>
<p>The commands we are going to execute for building the LaTeX files are dependent on Perl. As the Windows operating system doesn’t come with a built-in Perl compiler, we are going to install the <a target="_blank" href="https://strawberryperl.com/">Strawberry Perl</a>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972724660/7a6ad623-c2cc-45bd-bac6-08afdd5512c1.png" alt="Perl" class="image--center mx-auto" width="1902" height="897" loading="lazy"></p>
<p>Download the latest MSI package from it.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972749178/33c95897-37af-4be7-a24f-3520c3c1486e.png" alt="Download Strawberry perl" class="image--center mx-auto" width="1361" height="815" loading="lazy"></p>
<p>Install the executable after it gets finished downloading the application.</p>
<p>We need to add Perl’s path to the system environment. To do that, go into the location where it has been installed. By default, it gets installed inside <code>C:\Strawberry\perl\bin</code> directory. Copy the path.</p>
<p>Now search for “env” in the Windows search bar until you find something called “Edit the system environment variable”.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972898818/7cfdab3a-9ad0-47a7-b0ed-7721b589de97.png" alt="env" class="image--center mx-auto" width="810" height="678" loading="lazy"></p>
<p>Now click on “Environment Variables…”.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739972945973/832e011b-0841-4318-a9b8-8b7a8ae42819.png" alt="System Properties" class="image--center mx-auto" width="434" height="485" loading="lazy"></p>
<p>Now select “Path” from “System variables” and click “Edit”.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739973034756/df3d91f0-907e-42bf-9f1d-883172abd268.png" alt="System variables" class="image--center mx-auto" width="656" height="605" loading="lazy"></p>
<p>Click “New”. Paste the path. Now exit every windows sequentially by clicking on “OK” in each window.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739973087965/b73dd5e2-5c35-4399-a645-cb92ba43fe7b.png" alt="add var inside system path" class="image--center mx-auto" width="541" height="497" loading="lazy"></p>
<h2 id="heading-visual-code-studio-extensions">Visual Code Studio Extensions</h2>
<p>We need some extensions in VS Code to streamline our workflow.</p>
<p>First, let’s get <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop">LaTeX Workshop</a>. It is the core extension for working with LaTeX files inside VS Code Studio.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739973174197/2311c19b-d56e-4363-a3c0-75a9b0a323ee.png" alt="2311c19b-d56e-4363-a3c0-75a9b0a323ee" class="image--center mx-auto" width="791" height="518" loading="lazy"></p>
<p>Next, you’ll need <a target="_blank" href="https://marketplace.visualstudio.com/items?itemName=stkb.rewrap">Rewrap</a>. It is an amazing tool that lets you wrap longer lines. It helps you work in a long line in separate lines without breaking any structure or sentence.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739973216887/86bacaa6-77ff-441c-acca-08ee6a74d354.png" alt="86bacaa6-77ff-441c-acca-08ee6a74d354" class="image--center mx-auto" width="797" height="641" loading="lazy"></p>
<h2 id="heading-build-the-latex-file">Build the LaTeX File</h2>
<p>Whenever you want to build any LaTeX file inside VS Code studio, simply open that file in it. Then open the command palette using <code>Ctrl</code> + <code>Shift</code> + <code>P</code>.</p>
<p>Search for “LaTeX Workshop: Build with recipe” and go there. It will start building the file. Whenever it prompts you to install any missing package, untick the box that says “Always show this dialog” and press “Install”. I do this because clicking on “Install” on hundreds of prompt windows for building a LaTeX file is very difficult for me.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1739973393900/0ec3a626-38bb-4fbd-8f98-658cb6bc4853.png" alt="package installation" class="image--center mx-auto" width="516" height="371" loading="lazy"></p>
<p>After it finishes building the LaTeX file, you will get the output PDF file inside VS Code. You can open the PDF file directly in VS Code.</p>
<p>If you want to go into any specific line in the code from the output PDF file like Overleaf, simply click on that specific portion in the PDF by pressing the <code>Ctrl</code> key. It will immediately take you to the code part where it belongs.</p>
<p>That’s it! It’s now running on your local machine and there are no restrictions or limitation to it, literally! Also, for collaboration and keeping track of the history, using Git is the best option, like I do.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Thanks for reading this short tutorial. I hope it helped you interact more easily with LaTeX.</p>
<p>You can follow me on <a target="_blank" href="https://github.com/FahimFBA">GitHub</a>, <a target="_blank" href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a>, and <a target="_blank" href="https://www.linkedin.com/in/fahimfba/">You</a><a target="_blank" href="https://youtube.com/@FahimAmin">Tube</a> to get more content like this. Also, my <a target="_blank" href="https://www.fahimbinamin.com/">website</a> is always available for you!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What is a Floating-Point Arithmetic Problem? ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever worked with numbers like 1/3, where the result is 0.33333… and continues forever? As humans, we naturally round off such numbers, but have you ever wondered how computers handle them? In this article, you’ll explore how computers manage... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-a-floating-point-arithmetic-problem/</link>
                <guid isPermaLink="false">671a576a1d53cf0b6813a51c</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Maths ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ float ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Syeda Maham Fahim ]]>
                </dc:creator>
                <pubDate>Thu, 24 Oct 2024 14:19:22 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1729588035734/9824633d-727a-49ce-9080-0fa3a7b18ed6.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever worked with numbers like 1/3, where the result is 0.33333… and continues forever? As humans, we naturally round off such numbers, but have you ever wondered how computers handle them?</p>
<p>In this article, you’ll explore how computers manage continuous values, including the concept of precision errors. We’ll examine the floating-point arithmetic problem, a universal issue that affects many programming languages. We’ll focus specifically on how JavaScript addresses this challenge.</p>
<p>Additionally, you’ll learn how binary operations work behind the scenes, the threshold at which JavaScript truncates numbers based on the IEEE 754 standard, and introduce <code>BigInt</code> as a solution for accurately handling larger numbers without precision loss.</p>
<p>First, let's consider an example. Can you guess the output of this operation?</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span>);
</code></pre>
<p>You may think the answer is 0.3, right? But no, the actual output is:</p>
<pre><code class="lang-javascript">Output: <span class="hljs-number">0.30000000000000004</span>
</code></pre>
<p>You must be wondering why this is happening. Why so many extra zeros, and why does it end with a 4?</p>
<p>The answer is simple: the numbers 0.1 and 0.2 cannot be precisely represented in JavaScript (that is, "exactly" or "accurately.")</p>
<p>It sounds simple, right? But the explanation is a bit more complex.</p>
<p>So, what do you think—bug or feature?</p>
<p>Well, it’s not a bug. It’s actually a fundamental issue with how computers handle numbers, specifically floating-point numbers.</p>
<h2 id="heading-why-does-this-happen">Why Does This Happen?</h2>
<p>Let’s understand this with basic math.</p>
<p>The fraction 1/3 is represented in decimal by 0.33333... and it never ends. This means that 3 repeats itself infinitely. We can’t write it down exactly, so we approximate it to something like 0.333 or 0.333333 to save time and space.</p>
<p>Similarly, in a computer, we also have to approximate because 1/3 or 0.3333... would be a very large number and take up infinite space (which we don’t have).</p>
<p>This leads to what we call the floating-point arithmetic problem.</p>
<h2 id="heading-floating-point-arithmetic-problem">Floating-Point Arithmetic Problem</h2>
<p>In simple terms, floating-point numbers are numbers that cannot be written down exactly, so we approximate them. In a computer, this kind of approximation can lead to small precision errors, which we call the floating-point arithmetic problem.</p>
<h2 id="heading-binary-explanation">Binary Explanation</h2>
<p>Now that we've covered the simple explanation, let’s also understand this in binary terms. JavaScript handles everything in binary behind the scenes.</p>
<p>Binary is a number system that only uses two digits: 0 and 1.</p>
<h3 id="heading-why-cant-01-and-02-be-represented-exactly-in-binary">Why Can’t 0.1 and 0.2 Be Represented Exactly in Binary?</h3>
<p>The core issue is that not all decimal numbers can be perfectly represented as binary fractions.</p>
<p>Let’s take 0.1 as an example:</p>
<p>When you try to represent 0.1 in binary, you’ll find out that it can’t be expressed as a finite binary fraction. Instead, it becomes a repeating fraction, much like how 1/3 in decimal becomes 0.333..., repeating forever.</p>
<p>In binary, 0.1 becomes:</p>
<pre><code class="lang-plaintext">0.0001100110011001100110011001100110011... (repeating infinitely)
</code></pre>
<p>Since computers have limited memory, they can’t store this infinite sequence exactly. Instead, they have to cut off the number at some point, which introduces a small rounding error. This is why 0.1 in binary is only an approximation of the actual value.</p>
<p>Like 0.1, 0.2 can’t be exactly represented in binary. It becomes:</p>
<pre><code class="lang-plaintext">0.00110011001100110011001100110011... (repeating infinitely)
</code></pre>
<p>Again, the computer truncates (cutting off part of a number to fit a limit or remove extra digits) this infinite binary sequence, leading to a small error in representation.</p>
<p>So, what happens when we add 0.1 + 0.2? When you add 0.1 + 0.2 in JavaScript, the binary approximations for 0.1 and 0.2 are added together. But since both values are only approximations, the result is also an approximation.</p>
<p>Instead of getting exactly 0.3, you get something close to this:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-number">0.1</span> + <span class="hljs-number">0.2</span>); <span class="hljs-comment">// Output: 0.30000000000000004</span>
</code></pre>
<p>This slight error occurs because neither 0.1 nor 0.2 can be represented exactly in binary, so the final result has a small rounding error.</p>
<h2 id="heading-how-does-javascript-truncate-the-number">How Does JavaScript Truncate the Number?</h2>
<p>Now, the question arises: how does JavaScript know when to truncate the value?</p>
<p><strong>( Truncation</strong> means cutting off or shortening a number by removing extra digits beyond a certain point. <strong>)</strong></p>
<p>There’s a maximum and minimum limit for it.</p>
<p>To handle this in the computer world, we have a standard that defines how floating-point numbers are stored and calculated.</p>
<h2 id="heading-ieee-754-standard">IEEE 754 Standard</h2>
<p>JavaScript uses the IEEE 754 standard to handle floating-point arithmetic.</p>
<p>The standard defines safe integer limits for the <code>Number</code> type in JavaScript without precision loss:</p>
<ul>
<li><p><strong>Maximum Safe Integer:</strong> 2^53 - 1 or 9007199254740991</p>
</li>
<li><p><strong>Minimum Safe Integer:</strong> -(2^53 - 1) or -9007199254740991</p>
</li>
</ul>
<p>Beyond these limits, JavaScript cannot accurately represent integers due to the way floating-point arithmetic works.</p>
<p>For this reason, JavaScript provides two constants to represent these limits:</p>
<ul>
<li><p><code>Number.MAX_SAFE_INTEGER</code></p>
</li>
<li><p><code>Number.MIN_SAFE_INTEGER</code></p>
</li>
</ul>
<h3 id="heading-what-if-i-need-a-bigger-number">What If I Need a Bigger Number?</h3>
<p>If you need to work with numbers larger than the Maximum Safe Integer (like those used in cryptography or finance), JavaScript has a solution: BigInt.</p>
<h3 id="heading-enter-bigint">Enter BigInt</h3>
<p><code>BigInt</code> is a built-in object that allows you to work with whole numbers beyond the safe integer limit. It enables you to represent numbers larger than 9007199254740991, so you don't need to worry about precision errors here!</p>
<p>To use <code>BigInt</code>, simply append an <code>n</code> to the end of an integer literal:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> bigNumber = <span class="hljs-number">1234567890123456789012345678901234567890n</span>;
</code></pre>
<p>Alternatively, you can use the <code>BigInt</code> constructor:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> bigNumber = BigInt(<span class="hljs-string">"1234567890123456789012345678901234567890"</span>);
</code></pre>
<h4 id="heading-operations-with-bigint">Operations with BigInt</h4>
<p>You can perform arithmetic operations with <code>BigInt</code>, like addition, subtraction, multiplication, and even exponentiation. However, there’s a catch: you can’t mix <code>BigInt</code> with regular <code>Number</code> types in arithmetic operations without explicitly converting between them.</p>
<p>For example, this won’t work:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> result = bigNumber + <span class="hljs-number">5</span>; <span class="hljs-comment">// Error: cannot mix BigInt and other types</span>
</code></pre>
<p>You would need to convert the <code>Number</code> to <code>BigInt</code> first:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> result = bigNumber + BigInt(<span class="hljs-number">5</span>); <span class="hljs-comment">// Now it works!</span>
</code></pre>
<h3 id="heading-where-do-we-use-bigint">Where Do We Use BigInt?</h3>
<p><code>BigInt</code> is particularly useful in areas requiring precision, such as:</p>
<ul>
<li><p>Cryptographic algorithms</p>
</li>
<li><p>Handling large datasets</p>
</li>
<li><p>Financial calculations requiring exactness</p>
</li>
</ul>
<h3 id="heading-in-summary">In Summary</h3>
<ul>
<li><p>The safe integer limit in JavaScript ensures accurate number representation for integers between -(2^53 - 1) and 2^53 - 1.</p>
</li>
<li><p>Precision errors occur due to floating-point arithmetic when handling certain numbers (like 0.1 + 0.2).</p>
</li>
<li><p>If you need numbers bigger than the safe limit, <code>BigInt</code> is your friend. But remember, mixing <code>BigInt</code> and <code>Number</code> types require explicit conversions.</p>
</li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Blend Images in Rust Using Pixel Math ]]>
                </title>
                <description>
                    <![CDATA[ For anyone looking to learn about image processing as a programming niche, blending images is a very good place to start. It's one of the simplest yet most rewarding techniques when it comes to image processing. To help your intuition, it's best to i... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-blend-images-in-rust-using-pixel-math/</link>
                <guid isPermaLink="false">66cda9b4e220ecb31e3a2239</guid>
                
                    <category>
                        <![CDATA[ Rust ]]>
                    </category>
                
                    <category>
                        <![CDATA[ image processing ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Anshul Sanghi ]]>
                </dc:creator>
                <pubDate>Tue, 27 Aug 2024 10:25:56 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1724689572465/f03e4b74-1091-4673-af5b-c8827e74caf0.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>For anyone looking to learn about image processing as a programming niche, blending images is a very good place to start. It's one of the simplest yet most rewarding techniques when it comes to image processing.</p>
<p>To help your intuition, it's best to imagine an image as a mathematical graph of pixel values plotted along the x and y coordinates. The top right pixel in an image is your origin, which corresponds to an x value of 0 and a y value of 0.</p>
<p>Once you imagine this, any pixel in an image can be read or modified using it's coordinate in this x-y graph. For example, for a square image of size 5px x 5px, the coordinate of the center pixel is 2, 2. You may have expected it to be 3, 3, but image coordinates in this context work similar to array indexes and start from 0 for both axis.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724421916445/8d27ec1d-43f5-4cc3-b706-b9bd2efb05a4.png" alt="mathematical graph with x and y axis" class="image--center mx-auto" width="2786" height="1435" loading="lazy"></p>
<p>Approaching image processing this way also helps you address each pixel individually, making the process much simpler.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>The focus of this article is for you to understand and learn how to blend images using the Rust programming language, without going into the details of the language or it's syntax. So being comfortable writing Rust programs is required.</p>
<p>If you're not familiar with Rust, I highly encourage you to learn the basics. <a target="_blank" href="https://www.freecodecamp.org/news/rust-in-replit/">Here's an interactive Rust course that can get you started.</a></p>
<h2 id="heading-table-of-contents">Table Of Contents</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-introduction">Introduction</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-image-blending-works">How Image Blending Works</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-project-setup">Project Setup</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-read-pixel-values">How to Read Pixel Values</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-blend-functions">How to Blend Functions</a></p>
<ol>
<li><p><a class="post-section-overview" href="#heading-average-blend">Average Blend</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-multiply-blend">Multiply Blend</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-lighten-blend">Lighten Blend</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-darken-blend">Darken Blend</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-screen-blend">Screen Blend</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-addition-blend">Addition Blend</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-subtraction-blend">Subtraction Blend</a></p>
</li>
</ol>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-apply-blend-functions-to-images">How to Apply Blend Functions To Images</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-putting-it-all-together">Putting It All Together</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-glossary">Glossary</a></p>
</li>
</ol>
<h2 id="heading-introduction">Introduction</h2>
<p>Image blending refers to the technique of merging pixels from multiple images to create a single output image that is derived from all of its inputs. Depending on which blending operation is used, the image output can vary widely given the same inputs.</p>
<p>This technique serves as the basis for many complex image processing tools, some of which you may already be familiar with. Things such as removing moving people from images if you have multiple images, merging images of the night sky to create star trails, and merging multiple noise-heavy images to create a noise reduced image are all examples of this technique at play.</p>
<p>To achieve the blending of images in this tutorial, we will make use of "pixel math", which while not being a truly standard term, refers to the technique of performing mathematical operations on a pixel or set of pixels to generate an output pixel.</p>
<p>For example, to blend two images using the "average" blend mode, you will perform the mathematical average operation on all input pixels at a given location, to generate the output at the same location.</p>
<p>Pixel math is not limited to point operations, which are basically operations performed during image processing that generate a given output pixel based on input pixel from single or multiple images from the same location in the x-y coordinate system.</p>
<p>In my experience so far, the entirety of image processing field is 99% mathematics and 1% black magic. Mathematical operations on pixels and it's surrounding pixels is the basis of image manipulation techniques such as compression, resizing, blurring and sharpening, noise reduction, and so on.</p>
<h2 id="heading-how-image-blending-works"><strong>How Image Blending Works</strong></h2>
<p>The technique is technically simple to implement. Let's take the example of a simple average blend. Here's how it works:</p>
<ol>
<li><p>Read the pixel data of both images into memory, usually into an array for each image.</p>
<ul>
<li>The array is usually 2 dimensional. Each entry in array is another array for color images, the secondary array holds the 3 pixel values corresponding to Red, Green, and Blue color channels.</li>
</ul>
</li>
<li><p>For each pixel location:</p>
<ol>
<li><p>For each channel:<br> a. Take the value of the channel from the 2nd image, let's consider it <code>y</code>.<br> b. Perform the averaging operation <code>x/2 + y/2</code>.<br> c. Save the output value of this operation as the value of the output channel</p>
</li>
<li><p>Save the result of previous operation as the value of the output pixel.</p>
</li>
</ol>
</li>
<li><p>Construct the output image with the same dimensions from the computed data.</p>
</li>
</ol>
<p>You'll notice that pixel math is performed on a per-channel basis. This is always true for the blend modes we cover in this tutorial, but many techniques involve applying blends between the channels themselves and many times within the same image.</p>
<h2 id="heading-project-setup"><strong>Project Setup</strong></h2>
<p>Let's get started by setting up a project that gives us a good baseline to work with.</p>
<pre><code class="lang-bash">cargo new --bin image-blender
<span class="hljs-built_in">cd</span> image-blender
</code></pre>
<p>You will also need a single dependency to help you perform these operations:</p>
<pre><code class="lang-bash">cargo add image
</code></pre>
<p><code>image</code> is a Rust library we'll use to work with images of all of the standard formats and encodings. It also helps us convert between various formats, and provides easy access to pixel data as buffers.</p>
<p>For more information on the <code>image</code> crate, you can refer to the <a target="_blank" href="https://docs.rs/image/">official documentation</a>.</p>
<p>To follow along, you can use any two images, the only requirement being that they should be of the same size and in the same format. You can also find the images used in this tutorial, along with complete code, <a target="_blank" href="https://github.com/anshulsanghi-blog/blend-images">in the GitHub repository here</a>.</p>
<h2 id="heading-how-to-read-pixel-values"><strong>How to Read Pixel Values</strong></h2>
<p>The first step is to load the images and read their pixel values into a data structure that facilitates our operation. For this tutorial, we're going to use a <code>Vec</code> of arrays (<code>Vec&lt;[u8; 3]&gt;</code>). Each entry in the outer <code>Vec</code> represents a pixel, and the channel-wise values of each pixel are stored in <code>[u8; 3]</code> array.</p>
<p>Let's start by creating a new file to hold this code called <strong>io.rs</strong>.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/io.rs</span>

<span class="hljs-keyword">use</span> image::GenericImageView;

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">SourceData</span></span> {
    <span class="hljs-keyword">pub</span> width: <span class="hljs-built_in">usize</span>,
    <span class="hljs-keyword">pub</span> height: <span class="hljs-built_in">usize</span>,
    <span class="hljs-keyword">pub</span> image1: <span class="hljs-built_in">Vec</span>&lt;[<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]&gt;,
    <span class="hljs-keyword">pub</span> image2: <span class="hljs-built_in">Vec</span>&lt;[<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]&gt;,
}

<span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">read_pixel_data</span></span>(image1_path: <span class="hljs-built_in">String</span>, image2_path: <span class="hljs-built_in">String</span>) -&gt; SourceData {
    <span class="hljs-comment">// Open the images</span>
    <span class="hljs-keyword">let</span> image1 = image::open(image1_path).unwrap();
    <span class="hljs-keyword">let</span> image2 = image::open(image2_path).unwrap();

    <span class="hljs-comment">// Compute image dimensions</span>
    <span class="hljs-keyword">let</span> (width, height) = image1.dimensions();
    <span class="hljs-keyword">let</span> (width, height) = (width <span class="hljs-keyword">as</span> <span class="hljs-built_in">usize</span>, height <span class="hljs-keyword">as</span> <span class="hljs-built_in">usize</span>);

    <span class="hljs-comment">// Create arrays to hold input pixel data</span>
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> image1_data: <span class="hljs-built_in">Vec</span>&lt;[<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]&gt; = <span class="hljs-built_in">vec!</span>[[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]; width * height];
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> image2_data: <span class="hljs-built_in">Vec</span>&lt;[<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]&gt; = <span class="hljs-built_in">vec!</span>[[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>]; width * height];

    <span class="hljs-comment">// Iterate over all pixels in the input image, along with their positions in x &amp; y</span>
    <span class="hljs-comment">// coordinates.</span>
    <span class="hljs-keyword">for</span> (x, y, pixel) <span class="hljs-keyword">in</span> image1.to_rgb8().enumerate_pixels() {
        <span class="hljs-comment">// Compute the raw values for each channel in the RGB pixel.</span>
        <span class="hljs-keyword">let</span> [r, g, b] = pixel.<span class="hljs-number">0</span>;

        <span class="hljs-comment">// Compute linear index based on 2D index. This is basically computing index in</span>
        <span class="hljs-comment">// 1D array based on the row and column index of the pixel in the 2D image.</span>
        <span class="hljs-keyword">let</span> index = (y * (width <span class="hljs-keyword">as</span> <span class="hljs-built_in">u32</span>) + x) <span class="hljs-keyword">as</span> <span class="hljs-built_in">usize</span>;

        <span class="hljs-comment">// Save the channel-wise values in the correct index in data arrays.</span>
        image1_data[index] = [r, g, b];
    }

    <span class="hljs-comment">// Iterate over all pixels in the input image, along with their positions in x &amp; y</span>
    <span class="hljs-comment">// coordinates.</span>
    <span class="hljs-keyword">for</span> (x, y, pixel) <span class="hljs-keyword">in</span> image2.to_rgb8().enumerate_pixels() {
        <span class="hljs-comment">// Compute the raw values for each channel in the RGB pixel.</span>
        <span class="hljs-keyword">let</span> [r, g, b] = pixel.<span class="hljs-number">0</span>;

        <span class="hljs-comment">// Compute linear index based on 2D index. This is basically computing index in</span>
        <span class="hljs-comment">// 1D array based on the row and column index of the pixel in the 2D image.</span>
        <span class="hljs-keyword">let</span> index = (y * (width <span class="hljs-keyword">as</span> <span class="hljs-built_in">u32</span>) + x) <span class="hljs-keyword">as</span> <span class="hljs-built_in">usize</span>;

        <span class="hljs-comment">// Save the channel-wise values in the correct index in data arrays.</span>
        image2_data[index] = [r, g, b];
    }

    SourceData {
        width,
        height,
        image1: image1_data,
        image2: image2_data,
    }
}
</code></pre>
<h2 id="heading-how-to-blend-functions">How to Blend Functions</h2>
<p>The next step is to implement the blending functions, which are pure functions that take two pixel values as input and return the output value. This is implemented through the <code>BlendOperation</code> trait defined below. Let's create a new file to host all the operations called <strong>operations.rs</strong>.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/operations.rs</span>

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">trait</span> <span class="hljs-title">BlendOperation</span></span> {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">perform_operation</span></span>(&amp;<span class="hljs-keyword">self</span>, pixel1: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>], pixel2: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]) -&gt; [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>];
}
</code></pre>
<p>Next, we need to implement this trait for all of the blending methods we want to support.</p>
<p>For showcasing the result of each of the blending modes, the following two input images are blended together</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724236939605/77d32c76-abf6-4d24-bba7-df40729863b8.jpeg" alt="Source image 1: Fireflies in a dark forest area" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724428339241/3cc70fd2-f6da-4704-8606-97c094a2ff35.jpeg" alt="Source image 2: Fireflies in a bright forest area" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<h3 id="heading-average-blend">Average Blend</h3>
<p>An average blend involves channel-wise averaging the input pixel values to get the output pixel.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/operations.rs</span>

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">AverageBlend</span></span>;

<span class="hljs-keyword">impl</span> BlendOperation <span class="hljs-keyword">for</span> AverageBlend {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">perform_operation</span></span>(&amp;<span class="hljs-keyword">self</span>, pixel1: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>], pixel2: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]) -&gt; [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>] {
        [
            pixel1[<span class="hljs-number">0</span>] / <span class="hljs-number">2</span> + pixel2[<span class="hljs-number">0</span>] / <span class="hljs-number">2</span>,
            pixel1[<span class="hljs-number">1</span>] / <span class="hljs-number">2</span> + pixel2[<span class="hljs-number">1</span>] / <span class="hljs-number">2</span>,
            pixel1[<span class="hljs-number">2</span>] / <span class="hljs-number">2</span> + pixel2[<span class="hljs-number">2</span>] / <span class="hljs-number">2</span>,
        ]
    }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724236691772/291f14f4-2019-4771-8cd2-b9f9b3cf3f86.jpeg" alt="Result of average blending source images" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<h3 id="heading-multiply-blend">Multiply Blend</h3>
<p>A multiply blend involves channel-wise multiplication of input pixel values after they've been normalized<a class="post-section-overview" href="#heading-glossary">[¹]</a> to get the output pixel. The output pixel is then rescaled back to the original range by multiplying with 255.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/operations.rs</span>

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">MultiplyBlend</span></span>;

<span class="hljs-keyword">impl</span> BlendOperation <span class="hljs-keyword">for</span> MultiplyBlend {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">perform_operation</span></span>(&amp;<span class="hljs-keyword">self</span>, pixel1: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>], pixel2: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]) -&gt; [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>] {
        [
            ((pixel1[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>. * pixel2[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.) * <span class="hljs-number">255</span>.) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
            ((pixel1[<span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>. * pixel2[<span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.) * <span class="hljs-number">255</span>.) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
            ((pixel1[<span class="hljs-number">2</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>. * pixel2[<span class="hljs-number">2</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.) * <span class="hljs-number">255</span>.) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
        ]
    }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724236703622/9aff3ffd-9a63-4b76-9675-d7db4ccee89b.jpeg" alt="Result of multiply blending source images" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<h3 id="heading-lighten-blend">Lighten Blend</h3>
<p>Lighten blend involves channel-wise comparison of input pixel values, selecting the pixel with higher value (intensity) as the output pixel.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/operations.rs</span>

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">LightenBlend</span></span>;

<span class="hljs-keyword">impl</span> BlendOperation <span class="hljs-keyword">for</span> LightenBlend {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">perform_operation</span></span>(&amp;<span class="hljs-keyword">self</span>, pixel1: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>], pixel2: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]) -&gt; [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>] {
        [
            pixel1[<span class="hljs-number">0</span>].max(pixel2[<span class="hljs-number">0</span>]),
            pixel1[<span class="hljs-number">1</span>].max(pixel2[<span class="hljs-number">1</span>]),
            pixel1[<span class="hljs-number">2</span>].max(pixel2[<span class="hljs-number">2</span>]),
        ]
    }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724236726111/5d1607fb-2740-46b8-906d-1ffb482a0561.jpeg" alt="Result of lighten blending source images" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<h3 id="heading-darken-blend">Darken Blend</h3>
<p>Darken blend is the opposite operation of lighten blend. It involves channel-wise comparison of input pixel values, selecting the pixel with least value (intensity) as the output pixel.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/operations.rs</span>

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">DarkenBlend</span></span>;

<span class="hljs-keyword">impl</span> BlendOperation <span class="hljs-keyword">for</span> DarkenBlend {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">perform_operation</span></span>(&amp;<span class="hljs-keyword">self</span>, pixel1: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>], pixel2: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]) -&gt; [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>] {
        [
            pixel1[<span class="hljs-number">0</span>].min(pixel2[<span class="hljs-number">0</span>]),
            pixel1[<span class="hljs-number">1</span>].min(pixel2[<span class="hljs-number">1</span>]),
            pixel1[<span class="hljs-number">2</span>].min(pixel2[<span class="hljs-number">2</span>]),
        ]
    }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724236746972/18307fa1-1a77-4d39-b233-a7a6d87233d0.jpeg" alt="Result of darken blending source images" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<h3 id="heading-screen-blend">Screen Blend</h3>
<p>Screen blend refers to multiplying the inverse of two images, and then inverting the result. In our implementation, the pixels first need to be normalized<a class="post-section-overview" href="#heading-glossary">[¹]</a>. The normalized<a class="post-section-overview" href="#heading-glossary">[¹]</a> values are then inverted by subtracting them from 1, then they're multiplied and inverted again.</p>
<p>Finally, the output is multiplied by 255 to de-normalize the output pixel value.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/operations.rs</span>

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ScreenBlend</span></span>;

<span class="hljs-keyword">impl</span> BlendOperation <span class="hljs-keyword">for</span> ScreenBlend {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">perform_operation</span></span>(&amp;<span class="hljs-keyword">self</span>, pixel1: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>], pixel2: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]) -&gt; [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>] {
        [
            ((<span class="hljs-number">1</span>. - ((<span class="hljs-number">1</span>. - (pixel1[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.)) * (<span class="hljs-number">1</span>. - (pixel2[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.)))) * <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
            ((<span class="hljs-number">1</span>. - ((<span class="hljs-number">1</span>. - (pixel1[<span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.)) * (<span class="hljs-number">1</span>. - (pixel2[<span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.)))) * <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
            ((<span class="hljs-number">1</span>. - ((<span class="hljs-number">1</span>. - (pixel1[<span class="hljs-number">2</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.)) * (<span class="hljs-number">1</span>. - (pixel2[<span class="hljs-number">2</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span> / <span class="hljs-number">255</span>.)))) * <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">f32</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
        ]
    }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724236758380/fd531b6e-729c-4db4-987e-f503478ff950.jpeg" alt="Result of screen blending source images" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<h3 id="heading-addition-blend">Addition Blend</h3>
<p>Addition blend involves adding the input values and then clamping the result to the maximum range of the color depth we're targeting. In this case, that would be 0-255 as we're targeting 8-bit color depth.</p>
<p>We also have to convert the values to u16 in order to avoid loss of value due to overflow. We can also use normalized<a class="post-section-overview" href="#heading-glossary">[¹]</a> values here to achieve the same result.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/operations.rs</span>

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">AdditionBlend</span></span>;

<span class="hljs-keyword">impl</span> BlendOperation <span class="hljs-keyword">for</span> AdditionBlend {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">perform_operation</span></span>(&amp;<span class="hljs-keyword">self</span>, pixel1: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>], pixel2: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]) -&gt; [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>] {
        [
            (pixel1[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span> + pixel2[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span>).clamp(<span class="hljs-number">0</span>, <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
            (pixel1[<span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span> + pixel2[<span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span>).clamp(<span class="hljs-number">0</span>, <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
            (pixel1[<span class="hljs-number">2</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span> + pixel2[<span class="hljs-number">2</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span>).clamp(<span class="hljs-number">0</span>, <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">u16</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
        ]
    }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724236766684/05f01177-024d-4196-a9fa-5274bb56a0f4.jpeg" alt="Result of addition blending source images" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<h3 id="heading-subtraction-blend">Subtraction Blend</h3>
<p>Addition blend involves subtracting the input values and then clamping the result to the maximum range of the color depth we're targeting. In this case, that would be 0-255 as we're targeting 8-bit color depth.</p>
<p>We also convert the values to i16 in order to avoid loss of value due to overflow and lack of sign. We can also use normalized<a class="post-section-overview" href="#heading-glossary">[¹]</a> values here to achieve the same result.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/operations.rs</span>

<span class="hljs-keyword">pub</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">SubtractionBlend</span></span>;

<span class="hljs-keyword">impl</span> BlendOperation <span class="hljs-keyword">for</span> SubtractionBlend {
    <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">perform_operation</span></span>(&amp;<span class="hljs-keyword">self</span>, pixel1: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>], pixel2: [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>]) -&gt; [<span class="hljs-built_in">u8</span>; <span class="hljs-number">3</span>] {
        [
            (pixel1[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span> - pixel2[<span class="hljs-number">0</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span>).clamp(<span class="hljs-number">0</span>, <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
            (pixel1[<span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span> - pixel2[<span class="hljs-number">1</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span>).clamp(<span class="hljs-number">0</span>, <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
            (pixel1[<span class="hljs-number">2</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span> - pixel2[<span class="hljs-number">2</span>] <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span>).clamp(<span class="hljs-number">0</span>, <span class="hljs-built_in">u8</span>::MAX <span class="hljs-keyword">as</span> <span class="hljs-built_in">i16</span>) <span class="hljs-keyword">as</span> <span class="hljs-built_in">u8</span>,
        ]
    }
}
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1724236775603/507ba176-579d-494f-bb56-25a27ed2317f.jpeg" alt="Result of subtraction blending source images" class="image--center mx-auto" width="3000" height="1996" loading="lazy"></p>
<h2 id="heading-how-to-apply-blend-functions-to-images">How to Apply Blend Functions To Images</h2>
<p>The final step is to actually use the blending operations we created previously and apply them to pairs of images.</p>
<p>To achieve this, we need a function that can take the <code>SourceData</code> type we defined previously as input, along with a blending operation as the arguments, and gives us the final output buffer. Let's start by creating a new file for it called <strong>blend.rs</strong>.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/blend.rs</span>

<span class="hljs-keyword">use</span> image::{ImageBuffer, Rgb};
<span class="hljs-keyword">use</span> crate::{operations::BlendOperation, SourceData};

<span class="hljs-keyword">impl</span> SourceData {
    <span class="hljs-keyword">pub</span> <span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">blend_images</span></span>(&amp;<span class="hljs-keyword">self</span>, operation: <span class="hljs-keyword">impl</span> BlendOperation)  -&gt; ImageBuffer&lt;Rgb&lt;<span class="hljs-built_in">u8</span>&gt;, <span class="hljs-built_in">Vec</span>&lt;<span class="hljs-built_in">u8</span>&gt;&gt; {
        <span class="hljs-keyword">let</span> SourceData {
            width,
            height,
            image1,
            image2,
        } = <span class="hljs-keyword">self</span>;

        <span class="hljs-comment">// Create a new buffer that has the same size as input images, which will serve as our output data</span>
        <span class="hljs-keyword">let</span> <span class="hljs-keyword">mut</span> buffer = ImageBuffer::new(*width <span class="hljs-keyword">as</span> <span class="hljs-built_in">u32</span>, *height <span class="hljs-keyword">as</span> <span class="hljs-built_in">u32</span>);

        <span class="hljs-comment">// Iterate over all pixels in the output buffer, along with their coordinates</span>
        <span class="hljs-keyword">for</span> (x, y, output_pixel) <span class="hljs-keyword">in</span> buffer.enumerate_pixels_mut() {
            <span class="hljs-comment">// Compute linear index form x &amp; y coordinates. In other words, you have the</span>
            <span class="hljs-comment">// row and column indexes here, and you want to compute the array index based</span>
            <span class="hljs-comment">// on these two positions.</span>
            <span class="hljs-keyword">let</span> index = (y * *width <span class="hljs-keyword">as</span> <span class="hljs-built_in">u32</span> + x) <span class="hljs-keyword">as</span> <span class="hljs-built_in">usize</span>;

            <span class="hljs-comment">// Store pixel values in the given position into variables</span>
            <span class="hljs-keyword">let</span> pixel1 = image1[index];
            <span class="hljs-keyword">let</span> pixel2 = image2[index];

            <span class="hljs-comment">// Compute the blended pixel and convert it into the `Rgb` type, which is then</span>
            <span class="hljs-comment">// assigned to the output pixel in the buffer.</span>
            *output_pixel = Rgb::from(operation.perform_operation(pixel1, pixel2));
        }

        buffer
    }
}
</code></pre>
<h3 id="heading-putting-it-all-together">Putting It All Together</h3>
<p>It's now time to make use of all the new things you've learnt so far, and put them together in <strong>main.rs</strong> file.</p>
<pre><code class="lang-rust"><span class="hljs-comment">// src/main.rs</span>

<span class="hljs-keyword">mod</span> blend;
<span class="hljs-keyword">mod</span> io;
<span class="hljs-keyword">mod</span> operations;

<span class="hljs-keyword">use</span> io::*;
<span class="hljs-keyword">use</span> operations::{
    AdditionBlend, AverageBlend, DarkenBlend, LightenBlend, MultiplyBlend, ScreenBlend,
    SubtractionBlend,
};

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
    <span class="hljs-keyword">let</span> source_data = read_pixel_data(<span class="hljs-string">"image1.jpg"</span>.to_string(), <span class="hljs-string">"image2.jpg"</span>.to_string());

    <span class="hljs-keyword">let</span> output_buffer = source_data.blend_images(AdditionBlend);
    output_buffer.save(<span class="hljs-string">"addition.jpg"</span>).unwrap();

    <span class="hljs-keyword">let</span> output_buffer = source_data.blend_images(AverageBlend);
    output_buffer.save(<span class="hljs-string">"average.jpg"</span>).unwrap();

    <span class="hljs-keyword">let</span> output_buffer = source_data.blend_images(DarkenBlend);
    output_buffer.save(<span class="hljs-string">"darken.jpg"</span>).unwrap();

    <span class="hljs-keyword">let</span> output_buffer = source_data.blend_images(LightenBlend);
    output_buffer.save(<span class="hljs-string">"lighten.jpg"</span>).unwrap();

    <span class="hljs-keyword">let</span> output_buffer = source_data.blend_images(MultiplyBlend);
    output_buffer.save(<span class="hljs-string">"multiply.jpg"</span>).unwrap();

    <span class="hljs-keyword">let</span> output_buffer = source_data.blend_images(ScreenBlend);
    output_buffer.save(<span class="hljs-string">"screen.jpg"</span>).unwrap();

    <span class="hljs-keyword">let</span> output_buffer = source_data.blend_images(SubtractionBlend);
    output_buffer.save(<span class="hljs-string">"subtraction.jpg"</span>).unwrap();
}
</code></pre>
<p>You can now run the program using the following command, and you should have all the images generated and saved in the project folder:</p>
<pre><code class="lang-bash">cargo run --release
</code></pre>
<p>As you might have guessed already, this implementation only works for 8-bit RGB images. This code, however, can be extended very easily to support the other color formats such as 8-bit Luma (Monochrome), 16-bit RGB (Many RAW camera images), and so on.</p>
<p>I highly encourage you to try that out. You can also reach out to me for help with anything in this tutorial or with extending the code in this tutorial. I'd be happy to answer all your queries. Email is the best way to reach me, you can email me at <a target="_blank" href="mailto:anshul@anshulsanghi.tech">anshul@anshulsanghi.tech</a>.</p>
<h3 id="heading-glossary">Glossary</h3>
<p>Normalization refers to the process of rescaling the pixel values so that the values are in floating point format and are in the range of 0-1. For example, for an 8 bit image, the color black is represented by 0 (0 in de-normalized value) and the color white is represented by 1 (255 in de-normalized value). Intermediary decimal values between 0 &amp; 1 represent different intensities of the pixel between black and white. Normalization is done for many different reasons such as:</p>
<ul>
<li><p>Preventing overflows during calculations.</p>
</li>
<li><p>Re-scaling images to the same range irrespective of their individual color depth.</p>
</li>
<li><p>Expanding possible dynamic range of the image.</p>
</li>
</ul>
<h3 id="heading-enjoying-my-work"><strong>Enjoying my work?</strong></h3>
<p>Consider buying me a coffee to support my work!</p>
<p><a target="_blank" href="https://www.buymeacoffee.com/anshulsanghi"><img src="https://img.buymeacoffee.com/button-api/?text=Buy%20me%20a%20coffee&amp;emoji=%E2%98%95&amp;slug=anshulsanghi&amp;button_colour=FFDD00&amp;font_colour=000000&amp;font_family=Cookie&amp;outline_colour=000000&amp;coffee_colour=ffffff" alt="?text=Buy%20me%20a%20coffee&amp;emoji=%E2%98%95&amp;slug=anshulsanghi&amp;button_colour=FFDD00&amp;font_colour=000000&amp;font_family=Cookie&amp;outline_colour=000000&amp;coffee_colour=ffffff" width="235" height="50" loading="lazy"></a></p>
<p>Till next time, happy coding and wishing you clear skies!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn College Precalculus with Python ]]>
                </title>
                <description>
                    <![CDATA[ Precalculus is an important mathematical course that lays the groundwork for calculus and other higher-level math subjects. We just released a college precalculus course on the freeCodeCamp.org YouTube channel. This course is unique in that you will ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-college-precalculus-with-python/</link>
                <guid isPermaLink="false">6647aaebde814d2f7b14b3ba</guid>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Fri, 17 May 2024 19:07:23 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1715972815207/e6697fc1-8928-4ef5-9c8d-c9c8e8d01eaf.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Precalculus is an important mathematical course that lays the groundwork for calculus and other higher-level math subjects.</p>
<p>We just released a college precalculus course on the freeCodeCamp.org YouTube channel. This course is unique in that you will also learn to implement the concepts using the Python programming language. This course is ideal for students gearing up for college-level mathematics or anyone looking to strengthen their understanding of precalculus.</p>
<p>Ed Pratowski developed this course. He is an experienced mathematics instructor at a University in Pennsylvania.</p>
<p>Precalculus covers a variety of topics such as functions, trigonometry, matrices, and complex numbers. A solid grasp of precalculus is essential for success in fields like engineering, physics, computer science, and more.</p>
<p>In this course, you'll gain a deep understanding of essential precalculus concepts. Here’s a brief overview of what you’ll learn:</p>
<ul>
<li><p><strong>Data Analysis and Graphing:</strong> Learn to gather, interpret, and graph data effectively.</p>
</li>
<li><p><strong>Linear Equations:</strong> Understand how to draw and analyze lines, slopes, and intercepts.</p>
</li>
<li><p><strong>Trigonometry:</strong> Dive into the basics of trigonometry, solve right triangles, and explore degrees, radians, and the unit circle.</p>
</li>
<li><p><strong>Trigonometric Functions:</strong> Graph the six trigonometric functions, transform these graphs, and apply trigonometry to real-world problems.</p>
</li>
<li><p><strong>Triangles:</strong> Master the Law of Sines and Cosines, calculate the area of any triangle, and solve triangles from given points.</p>
</li>
<li><p><strong>Matrices:</strong> Perform operations with matrices, including addition, subtraction, multiplication, and finding inverses. Use matrices to solve systems of equations and develop equations.</p>
</li>
<li><p><strong>Complex Numbers and Series:</strong> Understand complex numbers, graph the Mandelbrot set, and explore series, including the number e.</p>
</li>
<li><p><strong>Probability and Applications:</strong> Learn probability theory, Pascal's triangle, and the math behind gambling.</p>
</li>
<li><p><strong>Practical Applications:</strong> Apply trigonometry to build a clock, encode and decode messages, and more.</p>
</li>
<li><p><strong>Advanced Topics:</strong> Simplify trigonometric expressions and discover Euler's identity, known as the beautiful math formula.</p>
</li>
</ul>
<p>This course offers clear explanations and practical examples to ensure you grasp these foundational concepts. Watch the full course on <a target="_blank" href="https://youtu.be/Y8oZtFYweTY">the freeCodeCamp.org YouTube channel</a> (12-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/Y8oZtFYweTY" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Apply Math with Python – Numerical Analysis Explained ]]>
                </title>
                <description>
                    <![CDATA[ Numerical analysis is the bridge between math and computer science.  Essentially, it is the development of algorithms that approximate solutions that pure math would also solve, but using less computational resources and faster. This field is very im... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/numerical-analysis-explained-how-to-apply-math-with-python/</link>
                <guid isPermaLink="false">66ba533e80dbd3f269f5887b</guid>
                
                    <category>
                        <![CDATA[ Math ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Tiago Capelo Monteiro ]]>
                </dc:creator>
                <pubDate>Thu, 29 Feb 2024 11:41:59 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/07/maxim-hopman-fiXLQXAhCfk-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Numerical analysis is the bridge between math and computer science. </p>
<p>Essentially, it is the development of algorithms that approximate solutions that pure math would also solve, but using less computational resources and faster.</p>
<p>This field is very important. Because for most solutions in the real world, we only need good approximations and not the exact solutions.</p>
<p>In this article, we will explore:</p>
<ul>
<li><a class="post-section-overview" href="#heading-an-analogy-that-illustrates-the-importance-of-numerical-analysis">Analogy Illustrating the Importance of Numerical Analysis</a> </li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/p/a66b15d8-ae59-4c46-8e58-5211690e1032/fundamentals">Fundamentals of Numerical Analysis</a> </li>
<li><a class="post-section-overview" href="#heading-application-of-numerical-analysis-in-real-world-problems">Application of Numerical Analysis in Real-World Problems</a></li>
<li><a class="post-section-overview" href="#heading-an-introduction-to-partial-differential-equations-pdes">Introduction to Partial Differential Equations (PDEs)</a></li>
<li><a class="post-section-overview" href="#heading-an-introduction-to-optimization-in-numerical-analysis">Introduction to Optimization in Numerical Analysis</a></li>
</ul>
<h2 id="analogy">An Analogy that Illustrates the Importance of Numerical Analysis</h2>

<p>How can we measure the coastline of an island?</p>
<p>If we try to measure every centimeter of every small segment, it would be impossible and probably time-consuming.</p>
<p>Because of the sea, the coastline is always changing at that level of detail.</p>
<p>However, by approximating and measuring in larger segments, we can get a practical measurement of the coastline.</p>
<p>This situation mirrors numerical analysis.</p>
<p>Approximation gives insights in situations where precise measurement is impossible or impractical.</p>
<p>Just as we accept a good estimation of the coastline length, numerical analysis uses approximation to solve hard problems.</p>
<h2 id="fundamentals">Fundamentals of Numerical Analysis</h2>

<p>Numerical analysis is all about approximation. It is like using binoculars to see a landscape that is very far away. We can't see every leaf. But we get a good enough picture to understand the terrain.</p>
<p>This is crucial in numerical analysis.</p>
<p>In this, we solve hard math problems where exact solutions are either impossible or extremely resource-intensive.</p>
<p>By approximating, we get sufficient good results with less computational effort.</p>
<h2 id="application">Application of Numerical Analysis in Real-World Problems</h2>

<p>There are many applications of numerical analysis</p>
<ul>
<li>In engineering, it enables simulation of structures and fluids.</li>
<li>In finance, for risk assessment and portfolio optimization.</li>
<li>In environmental science, it predicts climate patterns.</li>
</ul>
<p>In each field, numerical analysis is a toolkit to solve problems where pure math just takes too much time, or it is impossible to give good results.</p>
<h2 id="intro-PDE">An Introduction to Partial Differential Equations (PDEs)</h2>

<p>Partial Differential Equations (PDEs) are equations that describe how quantities like heat, sound, or electricity change in different places and as time goes on.</p>
<p>Solving PDEs is very important. Because it allows us to control these changes.</p>
<p>By allowing us to control them, we can:</p>
<ul>
<li>Predict weather patterns.</li>
<li>Understand sound propagation in different environments.</li>
<li>Design efficient transportation systems.</li>
<li>Optimize energy distribution.</li>
</ul>
<p>However, most PDE can only be approximated with numerical methods.</p>
<p>It is either too hard or impossible to find through normal calculations.</p>
<p>This way, with numerical methods, we are able to solve PDEs which in turn allows us to solve many real life problems.</p>
<h3 id="heading-numerical-solutions-of-pdes-with-scipy">Numerical Solutions of PDEs with SciPy</h3>
<p>Solving PDEs with numerical methods often involves dividing the PDEs in small, manageable parts. Solve each one and then add them up.</p>
<p>SciPy, a Python library for scientific and technical computing, gives many tools for this purpose.</p>
<p>Now, let's solve a heat transfer problem in a rod.</p>
<p>In the below code, we will see line by line how it allows us to know how heat spreads in a rod:</p>
<pre><code><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> scipy.integrate <span class="hljs-keyword">import</span> solve_bvp

def heat_equation(x, y):
    <span class="hljs-keyword">return</span> np.vstack((y[<span class="hljs-number">1</span>], -y[<span class="hljs-number">0</span>]))

def boundary_conditions(ya, yb):
    <span class="hljs-keyword">return</span> np.array([ya[<span class="hljs-number">0</span>], yb[<span class="hljs-number">0</span>] - <span class="hljs-number">1</span>])

x = np.linspace(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>)
y = np.zeros((<span class="hljs-number">2</span>, x.size))

sol = solve_bvp(heat_equation, boundary_conditions, x, y)
</code></pre><p>Lets see how thhe code works block by block in the following sections.</p>
<h3 id="heading-how-to-importing-libraries">How to importing libraries</h3>
<pre><code><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> scipy.integrate <span class="hljs-keyword">import</span> solve_bvp
</code></pre><p><img src="https://www.freecodecamp.org/news/content/images/2024/02/5-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>Importing libraries</em></p>
<p>Here we import 2 python libraries:</p>
<ul>
<li><a target="_blank" href="null">N</a><a target="_blank" href="https://numpy.org/">umPy</a></li>
<li><a target="_blank" href="null">S</a><a target="_blank" href="https://scipy.org/">ciPy</a></li>
</ul>
<p>These two python libraries are some of the most used in data science.</p>
<h3 id="heading-how-to-define-the-head-equation-and-boundary-conditions">How to define the head equation and boundary conditions</h3>
<pre><code>def heat_equation(x, y):
    <span class="hljs-keyword">return</span> np.vstack((y[<span class="hljs-number">1</span>], -y[<span class="hljs-number">0</span>]))

def boundary_conditions(ya, yb):
    <span class="hljs-keyword">return</span> np.array([ya[<span class="hljs-number">0</span>], yb[<span class="hljs-number">0</span>] - <span class="hljs-number">1</span>])
</code></pre><p><img src="https://www.freecodecamp.org/news/content/images/2024/02/6.png" alt="Image" width="600" height="400" loading="lazy">
<em>Defining heat equation and boundary conditions</em></p>
<p>We create <code>heat_equation(x, y)</code> and <code>boundary_conditions(ya, yb)</code>.</p>
<p>In <code>heat_equation(x, y)</code> we are defining the differential equation we want to solve.</p>
<p>The <code>boundary_conditions(ya, yb)</code> function defines constrains at the start and end of a solution. The condition is that the end of the solution needs to be one unit less than the start.</p>
<h3 id="heading-how-to-solve-the-equation">How to solve the equation</h3>
<pre><code>x = np.linspace(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>)
y = np.zeros((<span class="hljs-number">2</span>, x.size))

sol = solve_bvp(heat_equation, boundary_conditions, x, y)
</code></pre><p><img src="https://www.freecodecamp.org/news/content/images/2024/02/7.png" alt="Image" width="600" height="400" loading="lazy">
<em>Solving equation</em></p>
<p>The line <code>sol = solve_bvp(heat_equation, boundary_conditions, x, y)</code> is the solution.</p>
<p>The code <a target="_blank" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_bvp.html"><code>solve_bvp</code>  stands for solve boundary value problem</a>.</p>
<p>It takes four arguments:</p>
<ul>
<li><code>heat_equation</code>: This is the main problem we are trying to solve.</li>
<li><code>boundary_conditions</code>: These are the mathematical constrains at the start and end of a solution.</li>
<li><code>x</code>:  Are the spots we choose to explore our answers.</li>
<li><code>y</code>: Are initial attempts to solve the problem, based on your chosen <code>x</code> values.</li>
</ul>
<h2 id="intro-optimization">An Introduction to Optimization in Numerical Analysis</h2>

<p>Optimization is finding the best solution from all solutions. It is like finding the most efficient route in a complex network of roads.</p>
<p>Numerical optimization methods find the most efficient or cost-effective solution to a problem, whether that is:</p>
<ul>
<li>Minimizing waste in production.</li>
<li>Maximizing efficiency in a logistic network.</li>
<li>Finding best fit for a certain data model.</li>
</ul>
<h3 id="heading-an-overview-of-numerical-optimization-techniques-with-scipy">An Overview of Numerical Optimization Techniques with SciPy</h3>
<p>The goal in this example is to minimize transportation cost across a network. </p>
<p>For instance, let's consider an optimization problem in logistics, where the goal is to minimize transportation cost across a network. </p>
<p>SciPy's <code>minimize</code> function can be used to find the best strategy to minimizes cost while meeting all constraints:</p>
<pre><code><span class="hljs-keyword">from</span> scipy.optimize <span class="hljs-keyword">import</span> minimize

def objective_function(x):
    <span class="hljs-keyword">return</span> x[<span class="hljs-number">0</span>]**<span class="hljs-number">2</span> + x[<span class="hljs-number">1</span>]**<span class="hljs-number">2</span>

def constraint_eq(x):
    <span class="hljs-keyword">return</span> x[<span class="hljs-number">0</span>] + x[<span class="hljs-number">1</span>] - <span class="hljs-number">10</span>

con_eq = {<span class="hljs-string">'type'</span>: <span class="hljs-string">'eq'</span>, <span class="hljs-string">'fun'</span>: constraint_eq}

bounds = [(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>), (<span class="hljs-number">0</span>, <span class="hljs-number">10</span>)]

x0 = [<span class="hljs-number">5</span>, <span class="hljs-number">5</span>]

result = minimize(objective_function, x0, method=<span class="hljs-string">'SLSQP'</span>, bounds=bounds, constraints=[con_eq])
</code></pre><p>Lets explain how the code works block by block.</p>
<h3 id="heading-how-to-importing-the-library">How to importing the library</h3>
<pre><code><span class="hljs-keyword">from</span> scipy.optimize <span class="hljs-keyword">import</span> minimize
</code></pre><p><img src="https://www.freecodecamp.org/news/content/images/2024/02/8.png" alt="Image" width="600" height="400" loading="lazy">
<em>Importing scipy</em></p>
<p>Once again we import the necessary library:</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html">https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html</a></div>
<h3 id="heading-how-to-defining-objective-and-constraint-equation">How to defining objective and constraint equation</h3>
<pre><code>def objective_function(x):
    <span class="hljs-keyword">return</span> x[<span class="hljs-number">0</span>]**<span class="hljs-number">2</span> + x[<span class="hljs-number">1</span>]**<span class="hljs-number">2</span>

def constraint_eq(x):
    <span class="hljs-keyword">return</span> x[<span class="hljs-number">0</span>] + x[<span class="hljs-number">1</span>] - <span class="hljs-number">10</span>

con_eq = {<span class="hljs-string">'type'</span>: <span class="hljs-string">'eq'</span>, <span class="hljs-string">'fun'</span>: constraint_eq}
</code></pre><p><img src="https://www.freecodecamp.org/news/content/images/2024/02/9.png" alt="Image" width="600" height="400" loading="lazy">
<em>Define objective and constrain equations</em></p>
<ul>
<li>The objective function is the function we want to minimize to find the best answer.</li>
<li>The constraint equation is the equation that limits the search space to those <code>x</code> values that fulfill this equation.</li>
</ul>
<p><code>con_eq</code> is defined by the following:</p>
<ul>
<li><code>'type': 'eq'</code> indicates the type of constraint.  <code>'eq'</code> means equality, in other words, the function must equal zero at the solution.</li>
<li><code>'fun': constraint_eq</code> assigns the constraint function.</li>
</ul>
<p>We will see in the next block of code, it is where we constrain the possible solutions of the problem.</p>
<h3 id="heading-how-to-define-an-initial-condition-and-result">How to define an initial condition and result</h3>
<pre><code>bounds = [(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>), (<span class="hljs-number">0</span>, <span class="hljs-number">10</span>)]

x0 = [<span class="hljs-number">5</span>, <span class="hljs-number">5</span>]

result = minimize(objective_function, x0, method=<span class="hljs-string">'SLSQP'</span>, bounds=bounds, constraints=[con_eq])
</code></pre><p><img src="https://www.freecodecamp.org/news/content/images/2024/02/10.png" alt="Image" width="600" height="400" loading="lazy">
<em>Defining initial condition and solving equation</em></p>
<p>To understand this block of code, let's understand each parameter of <code>result = minimize(objective_function, x0, method='SLSQP', bounds=bounds, constraints=[con_eq])</code>:</p>
<ul>
<li><code>objective_function</code>: Is the function to be minimized.</li>
<li><code>x0</code>: Is the initial guess for the variables.</li>
<li><code>method='SLSQP'</code>: This specifies the optimization algorithm we are using. In this case, we use <a target="_blank" href="https://docs.scipy.org/doc/scipy/reference/optimize.minimize-slsqp.html">SLSQP (Sequential Least SQuares Programming)</a>.</li>
<li><code>bounds=bounds</code>: This parameter specifies the bounds for each of the decision variables. </li>
<li><code>constraints=[con_eq]</code>: This parameter tells us the constraints applied in the optimization problem.</li>
</ul>
<h2 id="heading-this-is-how-many-real-life-problems-are-solved">This is how many real life problems are solved</h2>
<p>Many things in real life are modeled with partial differential equation.</p>
<p>Then, with optimization methods developed with numerical analysis, they are optimized.</p>
<p>I am writing this because I know math can be boring for some people, and they may not be aware of where it is applied to solve real problems. The Calculus they learn can be applied in non-ideal situations outside the exams exercises.</p>
<p>Here, we can see finally see why math is important in two scenarios:</p>
<ul>
<li>To model systems to get solutions from it</li>
<li>To optimize a certain system</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Numerical analysis is one of the most important areas of applied math in STEM.</p>
<p>From solving PDE to optimize problems, numerical analysis is everywhere.</p>
<p>With more complex problems, numerical analysis is growing in importance to get faster algorithms that approximate pure math solutions.</p>
<p>This way, it is a bridge between theoretical mathematics and practical application.</p>
<p>If you want to, you can get the full code used in this article on <a target="_blank" href="https://github.com/tiagomonteiro0715/freecodecamp-my-articles-source-code">GitHub</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How the Euclidean Algorithm Works – with Code Examples in Go ]]>
                </title>
                <description>
                    <![CDATA[ By Otavio Ehrenberger The Euclidean Algorithm is a well-known and efficient method for finding the greatest common divisor (GCD) of two integers. The GCD is the largest number that can divide both integers without leaving a remainder.  The algorithm ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/euclidean-algorithm-in-golang/</link>
                <guid isPermaLink="false">66d851ede0db794d56c01bf9</guid>
                
                    <category>
                        <![CDATA[ algorithms ]]>
                    </category>
                
                    <category>
                        <![CDATA[ golang ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 08 May 2023 20:18:08 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/05/pexels-skitterphoto-1019470.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Otavio Ehrenberger</p>
<p>The Euclidean Algorithm is a well-known and efficient method for finding the greatest common divisor (GCD) of two integers. The GCD is the largest number that can divide both integers without leaving a remainder. </p>
<p>The algorithm is named after the ancient Greek mathematician Euclid, who presented it in his book "Elements" around 300 BCE.</p>
<p>You can use this algorithm to solve <a target="_blank" href="https://en.wikipedia.org/wiki/Diophantine_equation">Diophantine equations</a>, to tackle the <a target="_blank" href="https://en.wikipedia.org/wiki/Lattice_problem">shortest-vector problem</a> which is the foundation of <a target="_blank" href="https://en.wikipedia.org/wiki/Lattice-based_cryptography">lattice-based cryptography</a>, and also to detect common patterns of pixels in images. This is, among other things, applied to optimize rendering processes and detect different objects in images.</p>
<h2 id="heading-how-does-the-euclidean-algorithm-work">How Does the Euclidean Algorithm Work?</h2>
<p>Here's a step-by-step explanation of how the Euclidean Algorithm works:</p>
<p>Start with two positive integers, a and b, where a &gt;= b. If a &lt; b, simply swap their values. Note that this is meant for a convenient mathematical demonstration, as the implementation also works for a &lt; b.</p>
<p>Divide a by b and find the remainder, r (use the modulo operation, represented as a % b). If r is 0, the GCD is b, and the algorithm terminates.</p>
<p>If r is not 0, set a to b and b to r. Then, repeat step 2.</p>
<p>The algorithm continues to iterate until the remainder is 0. At that point, the last non-zero remainder is the GCD of the original two numbers. </p>
<p>The Euclidean Algorithm works because the GCD of two numbers remains unchanged when the larger number is replaced by its remainder when divided by the smaller number.</p>
<h3 id="heading-example-of-euclidean-algorithm">Example of Euclidean Algorithm</h3>
<p>Here's an example to illustrate the algorithm:</p>
<p>Let's find the GCD of 30 and 9:</p>
<p>a = 30, b = 9</p>
<p>Calculate the remainder: r = a % b = 30 % 9 = 3 (since 3 is not 0, continue to step 3)</p>
<p>Update the values: a = 9, b = 3</p>
<p>Calculate the new remainder: r = a % b = 9 % 3 = 0 (r is now 0)</p>
<p>The GCD of 30 and 9 is 3.</p>
<h2 id="heading-why-does-the-euclidean-algorithm-work">Why Does the Euclidean Algorithm Work?</h2>
<p>The greatest common divisor of two integers is the largest positive integer that divides both of them without leaving a remainder. So the algorithm is based on the following key property:</p>
<p><strong>If <code>a</code> and <code>b</code> are two integers, then the GCD of <code>a</code> and <code>b</code> is the same as the GCD of <code>b</code> and <code>a % b</code>, where <code>%</code> represents the modulo operator (the remainder after division)</strong>.</p>
<p>Mathematically, the key property of the algorithm can be justified using the division algorithm:</p>
<p>Let <code>a</code> and <code>b</code> be two positive integers, such that <code>a &gt;= b</code>. We can write the division algorithm as:</p>
<p><code>a = bq + r</code>, where <code>q</code> is the quotient and <code>r</code> is the remainder.</p>
<p>Now, let <code>d</code> be a common divisor of <code>a</code> and <code>b</code>. Then, <code>a = d * m1</code> and <code>b = d * m2</code> for some integers <code>m1</code> and <code>m2</code>. We can rewrite the division algorithm as:</p>
<p><code>d * m1 = (d * m2) * q + r</code>.</p>
<p>Rearranging the equation, we get:</p>
<p><code>r = d * (m1 - m2 * q)</code>.</p>
<p>Since <code>d</code> is a factor of both <code>a</code> and <code>b</code>, and <code>r</code> can also be written as a multiple of <code>d</code>, we can conclude that <code>d</code> is also a divisor of <code>r</code>. This means that the GCD of <code>a</code> and <code>b</code> is also a divisor of <code>r</code>. So, we can replace <code>b</code> with <code>r</code> and keep finding the GCD using this algorithm until <code>b</code> becomes 0.</p>
<p>The Euclidean Algorithm is particularly useful due to its efficiency and simplicity, making it easy to implement in computer algorithms and programming languages.</p>
<p>Let's see some different ways to implement it in Go:</p>
<h2 id="heading-recursive-implementation-of-the-euclidean-algorithm-in-go">Recursive Implementation of the Euclidean Algorithm in Go</h2>
<p>This implementation of the Euclidean Algorithm in Golang is a recursive version that finds the GCD of two integers. Let's go through it step by step:</p>
<p>The function is defined as <code>GCD(a, b int) int</code>. It takes two integer inputs, <code>a</code> and <code>b</code>, and returns an integer output.</p>
<p>The base case of the recursion is checked with <code>if b == 0</code>. If <code>b</code> is 0, the function returns the value of <code>a</code> as the GCD.</p>
<p>If <code>b</code> is not 0, a temporary variable <code>tmp</code> is created and assigned the value of <code>a</code>. This temporary variable is used to store the value of <code>a</code> before updating its value in the next step.</p>
<p>The values of <code>a</code> and <code>b</code> are updated as follows:</p>
<ul>
<li><code>a</code> is assigned the current value of <code>b</code>.</li>
<li><code>b</code> is assigned the value of the remainder when <code>tmp</code> (the previous value of <code>a</code>) is divided by the new value of <code>a</code> (which was <code>b</code> before the update).</li>
</ul>
<p>The function calls itself recursively with the updated values of <code>a</code> and <code>b</code> as input, <code>return GCD(a, b)</code>.</p>
<p>The algorithm continues to call itself recursively until the base case is reached, that is <code>b</code> becomes 0. At this point, the function returns the GCD, which is the value of <code>a</code>.</p>
<pre><code class="lang-go"><span class="hljs-comment">// Recursive approach:</span>
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">GCD</span><span class="hljs-params">(a, b <span class="hljs-keyword">int</span>)</span> <span class="hljs-title">int</span></span> {
    <span class="hljs-keyword">if</span> b == <span class="hljs-number">0</span> {
        <span class="hljs-keyword">return</span> a
    }
    tmp := a
    a = b
    b = tmp % a
    <span class="hljs-keyword">return</span> GCD(a, b)
}
</code></pre>
<p>For example, let's say we want to find the GCD of 56 and 48:</p>
<p>First call: GCD(56, 48)</p>
<ul>
<li>Since <code>b</code> (48) is not 0, update <code>a</code> and <code>b</code>:</li>
<li><code>a</code> becomes 48</li>
<li><code>b</code> becomes 56 % 48 = 8</li>
<li>The function calls itself with the new values: GCD(48, 8)</li>
</ul>
<p>Second call: GCD(48, 8)</p>
<ul>
<li>Since <code>b</code> (8) is not 0, update <code>a</code> and <code>b</code>:</li>
<li><code>a</code> becomes 8</li>
<li><code>b</code> becomes 48 % 8 = 0</li>
<li>The function calls itself with the new values: GCD(8, 0)</li>
</ul>
<p>Third call: GCD(8, 0)</p>
<ul>
<li>Now, <code>b</code> (0) is 0, so the function returns <code>a</code> (8) as the GCD.</li>
</ul>
<h2 id="heading-iterative-implementation-of-the-euclidean-algorithm-in-go">Iterative Implementation of the Euclidean Algorithm in Go</h2>
<p>This implementation of the Euclidean Algorithm in Golang is an iterative version using a loop to find the GCD of two integers. Let's go through the code step by step:</p>
<p>The function is defined as <code>GCD(a, b int) int</code>. It takes two integer inputs, <code>a</code> and <code>b</code>, and returns an integer output.</p>
<p>A loop is used to iterate as long as <code>b</code> is not equal to 0. The loop condition is <code>b != 0</code>. Note that this <code>for</code> loop construction in Go is essentially a <code>while</code> loop in many other languages.</p>
<p>Inside the loop, the values of <code>a</code> and <code>b</code> are updated simultaneously using a tuple assignment: <code>a, b = b, a%b</code>. This line does the following:</p>
<ul>
<li><code>a</code> is assigned the current value of <code>b</code>.</li>
<li><code>b</code> is assigned the value of the remainder when <code>a</code> is divided by <code>b</code>.</li>
</ul>
<p>When the loop exits (that is, <code>b</code> becomes 0), the value of <code>a</code> is returned as the GCD.</p>
<p>The algorithm iterates until the remainder (<code>b</code>) is 0, at which point the GCD is the last non-zero remainder, which is the value of <code>a</code>.</p>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">GCD</span><span class="hljs-params">(a, b <span class="hljs-keyword">int</span>)</span> <span class="hljs-title">int</span></span> {
    <span class="hljs-keyword">for</span> b != <span class="hljs-number">0</span> {
        a, b = b, a%b
    }
    <span class="hljs-keyword">return</span> a
}
</code></pre>
<p>For example, let's say we want to find the GCD of 100 and 64:</p>
<p>Initialize <code>a</code> as 100 and <code>b</code> as 64. Check the loop condition: <code>b</code> (64) is not 0.</p>
<p>Inside the loop, update <code>a</code> and <code>b</code>:</p>
<ul>
<li><code>a</code> becomes 64</li>
<li><code>b</code> becomes 100 % 64 = 36<br>Check the loop condition again: <code>b</code> (36) is not 0.</li>
</ul>
<p>Inside the loop, update <code>a</code> and <code>b</code>:</p>
<ul>
<li><code>a</code> becomes 36</li>
<li><code>b</code> becomes 64 % 36 = 28<br>Check the loop condition again: <code>b</code> (28) is not 0.</li>
</ul>
<p>Inside the loop, update <code>a</code> and <code>b</code>:</p>
<ul>
<li><code>a</code> becomes 28</li>
<li><code>b</code> becomes 36 % 28 = 8<br>Check the loop condition again: <code>b</code> (8) is not 0.</li>
</ul>
<p>Inside the loop, update <code>a</code> and <code>b</code>:</p>
<ul>
<li><code>a</code> becomes 8</li>
<li><code>b</code> becomes 28 % 8 = 4<br>Check the loop condition again: <code>b</code> (4) is not 0.</li>
</ul>
<p>Inside the loop, update <code>a</code> and <code>b</code>:</p>
<ul>
<li><code>a</code> becomes 4</li>
<li><code>b</code> becomes 8 % 4 = 0<br>Check the loop condition again: Now, <code>b</code> (0) is 0, so the loop exits.</li>
</ul>
<p>The function returns the value of <code>a</code> (4) as the GCD.</p>
<h2 id="heading-testing-the-solutions">Testing the Solutions</h2>
<p>These tests were created by Jon Calhoun in his free <a target="_blank" href="https://courses.calhoun.io/courses/cor_algo">Go Algorithms</a> course. Assuming you defined your <code>GCD</code> function in a file named <code>gcd.go</code>, place these tests in a file named <code>gcd_test.go</code>. Read below to see how the tests will work:</p>
<p>The function <code>TestGCD</code> is defined, taking a single parameter <code>t *testing.T</code>. The <code>*testing.T</code> is a pointer to a <code>testing.T</code> object that provides methods for reporting test failures and logging additional information.</p>
<p>An array of anonymous structs is defined, called <code>tests</code>. Each struct has three fields: <code>a</code>, <code>b</code>, and <code>want</code>. These structs represent test cases, where <code>a</code> and <code>b</code> are input values for the GCD function, and <code>want</code> is the expected result (correct GCD).</p>
<p>Several test cases are defined in the <code>tests</code> array, covering different scenarios.</p>
<p>A <code>for</code> loop iterates through the <code>tests</code> array. In each iteration, a single test case (struct) is assigned to the variable <code>tc</code>.</p>
<p>The <code>t.Run</code> function is called to run a subtest for the current test case. The first argument is a formatted string that describes the test case (using the input values <code>tc.a</code> and <code>tc.b</code>). The second argument is an anonymous function that takes a <code>*testing.T</code> parameter, similar to the main test function.</p>
<p>Inside the subtest function, the GCD function is called with the input values <code>tc.a</code> and <code>tc.b</code>, and the result is assigned to the <code>got</code> variable.</p>
<p>The <code>got</code> result is compared to the expected result <code>tc.want</code>. If they are not equal, the <code>t.Fatalf</code> function is called to report the test failure and provide an error message with the incorrect result and the expected result.</p>
<p>This test function helps ensure that the GCD function works correctly for various input values and edge cases. Running this test function with the <code>go test</code> command will execute all the test cases and report any failures, which can help identify potential issues with the GCD function implementation.</p>
<pre><code class="lang-go">
<span class="hljs-keyword">import</span> (
    <span class="hljs-string">"fmt"</span>
    <span class="hljs-string">"testing"</span>
)

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">TestGCD</span><span class="hljs-params">(t *testing.T)</span></span> {
    tests := []<span class="hljs-keyword">struct</span> {
        a, b <span class="hljs-keyword">int</span>
        want <span class="hljs-keyword">int</span>
    }{
        {<span class="hljs-number">10</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>},
        {<span class="hljs-number">25</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>},
        {<span class="hljs-number">30</span>, <span class="hljs-number">15</span>, <span class="hljs-number">15</span>},
        {<span class="hljs-number">30</span>, <span class="hljs-number">9</span>, <span class="hljs-number">3</span>},
        {<span class="hljs-number">100</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>},
        {
            <span class="hljs-number">2</span> * <span class="hljs-number">2</span> * <span class="hljs-number">3</span> * <span class="hljs-number">3</span> * <span class="hljs-number">5</span>,
            <span class="hljs-number">2</span> * <span class="hljs-number">3</span> * <span class="hljs-number">5</span> * <span class="hljs-number">7</span> * <span class="hljs-number">13</span>,
            <span class="hljs-number">2</span> * <span class="hljs-number">3</span> * <span class="hljs-number">5</span>,
        }, {
            <span class="hljs-number">2</span> * <span class="hljs-number">2</span> * <span class="hljs-number">3</span> * <span class="hljs-number">3</span> * <span class="hljs-number">13</span>,
            <span class="hljs-number">2</span> * <span class="hljs-number">3</span> * <span class="hljs-number">5</span> * <span class="hljs-number">7</span> * <span class="hljs-number">13</span>,
            <span class="hljs-number">2</span> * <span class="hljs-number">3</span> * <span class="hljs-number">13</span>,
        }, {
            <span class="hljs-number">2</span> * <span class="hljs-number">3</span> * <span class="hljs-number">5</span> * <span class="hljs-number">7</span> * <span class="hljs-number">11</span> * <span class="hljs-number">13</span> * <span class="hljs-number">17</span> * <span class="hljs-number">19</span>,
            <span class="hljs-number">3</span> * <span class="hljs-number">3</span> * <span class="hljs-number">7</span> * <span class="hljs-number">7</span> * <span class="hljs-number">11</span> * <span class="hljs-number">11</span> * <span class="hljs-number">17</span> * <span class="hljs-number">17</span>,
            <span class="hljs-number">3</span> * <span class="hljs-number">7</span> * <span class="hljs-number">11</span> * <span class="hljs-number">17</span>,
        },
    }
    <span class="hljs-keyword">for</span> _, tc := <span class="hljs-keyword">range</span> tests {
        t.Run(fmt.Sprintf(<span class="hljs-string">"(%v,%v)"</span>, tc.a, tc.b), <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(t *testing.T)</span></span> {
            got := GCD(tc.a, tc.b)
            <span class="hljs-keyword">if</span> got != tc.want {
                t.Fatalf(<span class="hljs-string">"GCD() = %v; want %v"</span>, got, tc.want)
            }
        })
    }
}
</code></pre>
<p>Run the tests, creating different cases as you wish:</p>
<pre><code class="lang-bash">go <span class="hljs-built_in">test</span> -v <span class="hljs-comment"># verbose flag</span>
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hope you had a good time learning about the Euclidean algorithm and its implementation in Go. </p>
<p>If you are interested in how these clever tricks work, take a look at the field of Number Theory in mathematics, which is particularly focused on the properties of integers, especially involving prime numbers.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Free College Algebra Course (with Python Code!) ]]>
                </title>
                <description>
                    <![CDATA[ College Algebra is an important course for a wide variety of professions and University programs. It is especially important for computer science students. We just published a full and comprehensive college Algebra course on the freeCodeCamp.org YouT... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/college-algebra-course-with-python-code/</link>
                <guid isPermaLink="false">66b20152297cd6de0bd545f3</guid>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Mon, 23 Jan 2023 17:00:26 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/01/algebra.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>College Algebra is an important course for a wide variety of professions and University programs. It is especially important for computer science students.</p>
<p>We just published a full and comprehensive college Algebra course on the freeCodeCamp.org YouTube channel. This course is designed as a full one-semester college course.</p>
<p>This course is different than other college Algebra courses out there. Sure it is taught by an experienced university professor like the other ones. And sure it covers all the required college Algebra concepts like the other ones. What sets this course apart is that you will learn how to implement all the Algebra concepts using the popular Python programming language.</p>
<p>Ed Pratowski teaches this course. He is an experienced university mathematics professor and an amazing Python developer.</p>
<p>This is the perfect Algebra course for anyone interested in fields related to computer science, data science, or machine learning. It can be helpful to already have some familiarity with Python or another programming language before starting this course. However, previous Python experience is not required.</p>
<p>This course is a combination of presentations in front of a whiteboard and programming Python code on a computer. All the coding is done in Google Colaboratory notebooks. This makes it easy for anyone to follow along on their own computer without installing any software.</p>
<p>As you go through each part of this course in sequence, watching each video and working through each Colab notebook, you will gain a full understanding of Algebra and how to write Python code to solve your Algebra problems.</p>
<p>Throughout this course, you will also build your own Algebra notebook that you will be able to use as your custom super calculator. This course (and the code you write here) will give you the foundation for deeper math, including data science.</p>
<p>The course is broken up into 15 "weeks". Here are the sections covered in this course:</p>
<h3 id="heading-week-one-ratios-proportions-and-conversions">Week One: Ratios, Proportions, and conversions</h3>
<ul>
<li>Intro to Algebra and variables</li>
<li>Setting up Colab and intro to code</li>
<li>Setting up a proportion</li>
<li>Solving a proportion</li>
<li>Solving proportions with code</li>
<li>Define n1, d1, n2, d2 variables</li>
<li>Use “if” statements for unknown numerator or denominator</li>
</ul>
<h3 id="heading-week-two-basic-algebra-solving-equations-one-variable">Week Two: Basic Algebra, solving equations (one variable)</h3>
<ul>
<li>solving for x</li>
<li>Four one-step equations</li>
<li>A Two-step equation</li>
<li>Setting equations equal to zero</li>
<li>Import statement</li>
<li>How Sympy works</li>
</ul>
<h3 id="heading-week-three-percents-decimals-and-fractions">Week Three: Percents, Decimals, and Fractions</h3>
<ul>
<li>Fraction, decimal, percent</li>
<li>“Out of” = fraction = divide</li>
<li>Decimal answer</li>
<li>Exponents and decimal places</li>
<li>“Percent” = “out of 100” = first two decimal places</li>
<li>Decimal to fraction by hand</li>
<li>Python code to convert decimal to fraction</li>
<li>Practice problems in Colab notebook</li>
</ul>
<h3 id="heading-week-4-math-function-definition-using-two-variables-xy">Week 4: Math function definition, using two variables (x,y)</h3>
<ul>
<li>Functions</li>
<li>What does it mean to be a “function”</li>
<li>Input and output, independent and dependent variables, x and y</li>
<li>Notation, y=, f(x)=, x-y table, find a few points</li>
<li>Notation, input and output</li>
<li>Loop to generate and print (x,y) values</li>
<li>Graphing</li>
<li>Intro to Cartesian plane and (x,y) coordinates</li>
<li>Equation, table, graph connection</li>
<li>Plotting points</li>
<li>Import matplot lib</li>
<li>How to set up and display a blank graph</li>
<li>How to plot one point</li>
<li>How to plot several points (that line up)</li>
<li>Define example</li>
<li>function</li>
<li>Loop to generate x and y arrays</li>
</ul>
<h3 id="heading-week-5-slope-and-intercept-on-a-graph">Week 5: Slope and intercept on a graph</h3>
<ul>
<li>Slope</li>
<li>What is slope?</li>
<li>Finding slope on a graph (counting boxes)</li>
<li>Slope formula (and how it relates to the graph)</li>
<li>Finding slope from two points</li>
<li>Finding slope from two points</li>
<li>Linear equations</li>
<li>Finding y-intercept on graph</li>
<li>Linear equation from graph</li>
<li>Graph line, given the linear equation</li>
<li>Developing equation from two points</li>
</ul>
<h3 id="heading-week-6-factoring-finding-common-factors-and-factoring-square-roots">Week 6: Factoring, finding common factors and factoring square roots</h3>
<ul>
<li>Common factors</li>
<li>Finding and dividing out common factors</li>
<li>Reducing fractions</li>
<li>Loops and factors</li>
<li>Using a loop and modulus to find factors</li>
<li>Dividing out factors</li>
<li>Factoring square roots</li>
</ul>
<h3 id="heading-week-7-graphing-systems-of-equations">Week 7: Graphing systems of equations</h3>
<ul>
<li>Graphing systems</li>
<li>Plotting two functions on same graph</li>
<li>See where the lines intersect</li>
<li>Import matplotlib</li>
<li>Import numpy</li>
<li>Linspace and x values</li>
<li>Plotting two functions</li>
</ul>
<h3 id="heading-week-8-solving-systems-of-two-equations">Week 8: Solving systems of two equations</h3>
<ul>
<li>Solving systems</li>
<li>When both functions are “y =”</li>
<li>Setting each function equal to zero</li>
<li>Connect with using this in the Python code</li>
<li>Sympy and numerical solutions when they each equal zero</li>
<li>Sympy factoring then graphing (without numpy or matplotlib)</li>
<li>Show how to solve AND graph AND plot solution point with one block of code</li>
</ul>
<h3 id="heading-week-9-applications-of-linear-systems">Week 9: Applications of linear systems</h3>
<ul>
<li>Word problems</li>
<li>Key words for add, subtract, multiply, and divide</li>
<li>Compare costs of train tickets, amusement parks, and other things</li>
<li>Show solving word problems</li>
<li>Identifying key words</li>
<li>Setting up equations</li>
</ul>
<h3 id="heading-week-10-quadratic-equations">Week 10: Quadratic equations</h3>
<ul>
<li>Quadratics</li>
<li>Standard form and a, b, and c coefficients</li>
<li>General shape of the graph</li>
<li>Vertex formula</li>
<li>Roots and quadratic formula</li>
<li>Defining a, b, c</li>
<li>Vertex formula, then solve for y</li>
<li>Quadratic formula, discriminant, solve for two roots</li>
<li>Graphs, based on abc input</li>
</ul>
<h3 id="heading-week-11-polynomial-graphs">Week 11: Polynomial Graphs</h3>
<ul>
<li>Parent graphs</li>
<li>Linear (y = x)</li>
<li>Quadratic (x2)</li>
<li>Cubic (x3)</li>
<li>Other exponents (x4, x5, x6, x7 and the pattern)</li>
<li>Floor function</li>
<li>Square root (and one-half exponent)</li>
<li>Cube root (rational exponent doesn’t work)</li>
<li>Absolute value</li>
<li>Exponential functions (like percent increase)</li>
</ul>
<h3 id="heading-week-12-cost-revenue-and-profit-equations">Week 12: Cost, Revenue, and Profit equations</h3>
<ul>
<li>Cost, revenue, profit</li>
<li>Example situation with cost, revenue, demand, and profit</li>
<li>Show how the equations relate</li>
<li>Show how the graphs relate</li>
<li>Define equations and combinations (like P = R - C)</li>
<li>Write code to answer application questions</li>
<li>What do the roots and vertex mean, applied to profit and revenue</li>
</ul>
<h3 id="heading-week-13-simple-and-compound-interest-formulas">Week 13: Simple and compound interest formulas</h3>
<ul>
<li>Simple and compound interest</li>
<li>Simple interest and when it it used</li>
<li>Simple interest plus principle = compound interest</li>
<li>Percent increase formula: P(1+r) with algebraic explanation</li>
<li>Compound interest formula: P(1+r)t with explanation</li>
<li>Compounded monthly formula: P(1 + r/n)^nt</li>
<li>Interest and annuity</li>
<li>Comparing simple and compound interest with a loop</li>
<li>Mortgage payment formula</li>
<li>Amortization table in a loop</li>
<li>Retirement investments in a loop</li>
</ul>
<h3 id="heading-exponents-and-logarithms">Exponents and logarithms</h3>
<ul>
<li>What logs are</li>
<li>Common logs</li>
<li>Natural logs</li>
<li>Inverses and canceling</li>
<li>Scientific notation</li>
<li>Notation and code</li>
<li>Log equations and canceling</li>
<li>Scientific notation</li>
<li>Graphing</li>
</ul>
<h3 id="heading-week-15-spreadsheets-and-additional-resources">Week 15: Spreadsheets and Additional Resources</h3>
<ul>
<li>Calculator In Google Chrome browser</li>
<li>Spreadsheet and formulas</li>
<li>Using Wolfram Alpha (and Google Chrome extension)</li>
<li>Desmos and Geogebra</li>
<li>Applications and practice</li>
</ul>
<p>Watch the full course on <a target="_blank" href="https://www.youtube.com/watch?v=i7vOAcUo5iA">the freeCodeCamp.org YouTube channel</a> (15 hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/i7vOAcUo5iA" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Calculate Percentage in Excel – Formula for Percentages ]]>
                </title>
                <description>
                    <![CDATA[ A percentage is a kind of fraction saying how many parts over 100 something is. In this article you will see in detail what a percentage is, and how to calculate a percentage in Excel. First, before diving into how it works in Excel, let's take a loo... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-calculate-percentage-in-excel-formula-for-percentages/</link>
                <guid isPermaLink="false">66b0c38ebb3f390180bed0d5</guid>
                
                    <category>
                        <![CDATA[ excel ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Math ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ MathJax ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ilenia Magoni ]]>
                </dc:creator>
                <pubDate>Thu, 18 Aug 2022 17:44:39 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/08/victoria-strukovskaya-OhL_qEqpef4-unsplash-1.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>A percentage is a kind of fraction saying how many parts over 100 something is. In this article you will see in detail what a percentage is, and how to calculate a percentage in Excel.</p>
<p>First, before diving into how it works in Excel, let's take a look at the math.</p>
<p>If you want to jump directly to the Excel part, and avoid the math part, please proceed to the section <a class="post-section-overview" href="#heading-how-to-work-with-percentages-in-excel">How to work with percentages in Excel</a>.</p>
<h2 id="heading-what-is-the-formula-for-calculating-a-percentage">What is the Formula for Calculating a Percentage?</h2>
<p>Percentages from 0% to 100% are equivalent to decimal values from 0 to 1. That means you can convert from a percentage to a decimal number. Let's see how.</p>
<h3 id="heading-how-to-convert-from-decimal-number-to-percentage">How to Convert from Decimal Number to Percentage</h3>
<p>You can convert from a decimal number to a percentage in the following way:</p>
<ul>
<li>take the decimal number, (d),</li>
<li>multiply it by (100),</li>
<li>and then add the percentage (\%) symbol,</li>
</ul>
<p>In short, if you want to get a percentage from a number (d) you can do this:</p>
<p>$$ ( d \cdot 100 ) \% $$</p><h3 id="heading-how-to-convert-from-a-percentage-to-a-decimal-number">How to Convert from a Percentage to a Decimal Number</h3>
<p>If you want to convert from a percentage to a number, you can:</p>
<ul>
<li>take the percentage (P\%),</li>
<li>remove the percentage symbol, (P)</li>
<li>then divide the number by (100): (\frac{P}{100})</li>
</ul>
<p>In short, to convert a percentage (P\%) to a decimal number:</p>
<p>$$ \frac{P\xcancel{\%}}{100} $$</p><h3 id="heading-how-to-calculate-the-percentage-of-a-number">How to Calculate the Percentage of a Number</h3>
<p>There are three cases in which you might want to calculate the percentage of a number:</p>
<ul>
<li>to add a percentage to the number (like for calculating how much you have to pay including VAT)</li>
<li>to just get the percentage of the number</li>
<li>to subtract a percentage from the number (like for a sale)</li>
</ul>
<h4 id="heading-how-to-add-a-percentage-to-a-number">How to add a percentage to a number</h4>
<p>Let's first use a specific case, and then let's generalise.</p>
<p>You went shopping, and the total you need to pay is $185 + VAT. Let's say VAT in this case is 17%.</p>
<p>So you would have to calculate 17% of $185 and then add it to $185.</p>
<p>$$ \$185  \cdot 17 \% + \$185 $$</p>
<p>You can make it a single operation if instead you multiply by ( 117 \% ).</p>
<p>$$ \$185 \cdot ( 100\% + 17\% ) = \$185 \cdot 117\% $$</p>
<p>If you have a simple calculator that doesn't have a percentage operator, you can convert the percentage to a decimal number as we discussed above. Here's what that would look like:</p>
<p>$$ \$185 \cdot 117\% = \$185 \cdot \frac{117 \xcancel{ \% }}{ 100 } = \$185 \cdot 1.17 $$</p>
<p>So, in general, if you want to add a percentage ( P \% ) to a number ( n ) you can do this:</p>
<p>$$ n \cdot ( 100\% + P\%) $$</p><p>Or if instead you have the percentage in the decimal number form ( d ) you can do this:</p>
<p>$$ n \cdot (1 + d) $$</p><h4 id="heading-how-to-calculate-the-percentage-of-a-number-1">How to calculate the percentage of a number</h4>
<p>If you want to buy a house you need to pay a deposit on the house. Let's say that's 15% of the total value of the house.</p>
<p>If the house price is $200,000, how can you calculate it?</p>
<p>You can multiply the price by the percent number to get the percentage.</p>
<p>$$ \$200,000 \cdot 15 \% = \$30,000 $$</p>
<p>So if you want to know what's the percentage (P\%) or fraction (d) of a number (m), you can multiply that number by the percentage or fraction.</p>
<p>$$ m \cdot P\% \newline \, \\ m \cdot d $$</p><h4 id="heading-how-to-subtract-a-percentage-from-a-number">How to subtract a percentage from a number</h4>
<p>This case is common for calculating the price of an item on sale. For example, if something has an original price of $999 and it is on sale for 20% less, you can calculate the the final price by figuring out what is 20% of $999. Well, it's $199.80, so then you just subtract that value from $999.</p>
<p>The final price of the item on sale is $799.20.</p>
<p>You can calculate it in a single step if you multiply the original price by ((100\% - 20\%)) or (80\%):</p>
<p>$$ \$999 \cdot (100\% - 20\%) = \$999 \cdot 80\% = \$779.20 $$</p>
<p>So the general formula to subtract a percentage (P\%) from a number (b) is:</p>
<p>$$ b \cdot (100\% - P\%) $$</p><p>Or in decimal form:</p>
<p>$$ b \cdot (1 - d) $$</p><h3 id="heading-how-to-calculate-the-percentage-of-a-number-in-respect-to-another-number">How to Calculate the Percentage of a Number in Respect to Another Number</h3>
<p>A cake weights 11 lbs, and you eat 3 pieces for a total of 21 oz. How can you know what percentage of cake have you eaten?</p>
<p>It's important to do calculations of this kind with everything in the same units. So, first, let's convert the weight of the cake to ounces.</p>
<p>$$ 11~\mathrm{lb} \cdot \frac{16~\mathrm{oz}}{\mathrm{lb}} = 176~\mathrm{oz} $$</p><p>Now it's possible to see the percentage of the cake you've eaten: you need to divide the part (in this case the 21 oz of cake you have eaten) by the whole (the 176 oz of the whole cake):</p>
<p>$$ \frac{11~\mathrm{oz}}{176~\mathrm{oz}} = 0.0625 $$</p><p>As seen in the paragraph on how to convert a decimal to a number, you can convert this to a percentage with ( 0.0625 \cdot 100 \% = 6.25\%).</p>
<p>In this example, you have eaten 6.25% of the whole cake.</p>
<h3 id="heading-how-do-you-combine-two-percentages">How Do You Combine Two Percentages?</h3>
<p>Let's say that some items in an online store are discounted at 20%, and then there is an offer for a 10% discount for everything in your cart at payment.</p>
<p>If you are getting a discount of 20% and then another 10% on top of that... how much is the discount?</p>
<p>It's not 30%, because the second discount is applied at a different time than the first discount.</p>
<p>A discount of 20% means you pay 80% ((100\% - 20\%)) of the original price, and a discount of 10% means you pay 90% ((100\% - 10\%))of the original price. Combining the two discounts means multiplying together the two percentages, like so:</p>
<p>$$ 80\% \cdot 90\% = 72\% $$</p><p>Or in decimal form:</p>
<p>$$ 0.80 \cdot 0.90 = 0.72 $$</p><p>So you pay 72% of the original price, or have a discount of 28%.</p>
<h2 id="heading-why-are-a-decimal-and-a-percentage-equivalent">Why Are a Decimal and a Percentage Equivalent?</h2>
<p>Percentage comes from "per cent", or "over one hundred". The percentage symbol represents the line fraction with 100 in the denominator.</p>
<p>$$ 0.20 = \frac{20}{100} = 20\% $$</p><p>The percentage is a simple way to describe how many hundredths of a whole something is.</p>
<h2 id="heading-how-to-work-with-percentages-in-excel">How to Work with Percentages in Excel</h2>
<p>Now it's time to start working with Excel. Let's start with a pretty important thing. How do you write percentages in Excel?</p>
<h3 id="heading-how-to-write-percentages-in-excel">How to Write Percentages in Excel</h3>
<p>In Excel you can write percentages in two ways:</p>
<ul>
<li>You can write a decimal number and then format the cell as a percentage from the format drop down menu or from the Percent Style button:</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/image-86.png" alt="Image" width="600" height="400" loading="lazy">
<em>In the first image the Percent Style button is circled in red, and a decimal number is written in the cell. In the second image, the cell has been formatted as a percentage, and the number is converted to a percentage.</em></p>
<ul>
<li>You can write a percentage in the cell, and the cell will automatically take the percentage formatting:</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/image-87.png" alt="Image" width="600" height="400" loading="lazy">
<em>In the first image you can see that the empty cell has the "General" formatting, in the second image you can see a percentage being written in the cell, and in the third image you can see that the cell has taken the percentage formatting.</em></p>
<p>The default format is for a whole percentage. You can change the number of decimal places shown with the "Increase Decimal" and "Decrease Decimal" buttons.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/image-112.png" alt="Image" width="600" height="400" loading="lazy">
<em>The two buttons "Increase Decimal" and "Decrease Decimal" circled in red.</em></p>
<p>Let's say you have a decimal form of 0.12345. Formatting it to a percentage will show ( 12\% ). You can use the buttons to show the decimal places.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/image-113.png" alt="Image" width="600" height="400" loading="lazy">
<em>In the first image, the value 12% appears in the cell K1. In the second image, after using the "Increase Decimal" button twice, the value 12.35% appears in the cell K1. In the third image, after using the button "Decrease Decimal" once, the value 12.3% appears in the cell K1.</em></p>
<h3 id="heading-how-to-calculate-the-percentage-of-a-number-in-excel">How to Calculate the Percentage of a Number in Excel</h3>
<p>In math, the percentage of a number is calculated by multiplying that number by the percentage: ( 87\% \cdot 824 = 716.88 ).</p>
<p>In Excel, let's say you have the number in cell A1 and the percentage in cell B1. To calculate the result, you can write in cell C1 <code>=A1*B1</code>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/image-110.png" alt="Image" width="600" height="400" loading="lazy">
<em>In the first image you can see the formula to calculate the percentage of a number, and then its result in the second image.</em></p>
<h3 id="heading-how-to-calculate-the-percentage-of-a-number-with-respect-to-another-in-excel">How to Calculate the Percentage of a Number with Respect to Another in Excel</h3>
<p>In math, you can calculate the percentage of a number with respect to another number by taking the ratio of the two numbers and then converting to a percentage.</p>
<p>If you want to know what percentage 75 is with respect to 142, you can do ( \frac{75}{142} = 0.528 = 52.8\% ).</p>
<p>Let's write 75 in cell G1, and 142 in cell H1. To get the percentage of 75 in respect of 142 you can write in cell I1 <code>=G1/H1</code>. The cell will have the result in decimal format, but you can format it to a percentage using the Percent Style button. Then you can adjust the number of decimal places with the Increase Decimal and Decrease Decimal buttons.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/image-111.png" alt="Image" width="600" height="400" loading="lazy">
<em>In the first image you can see the formula to do the calculation. In the second image it results in a decimal number. In the third image it has been converted to a percentage, and then in the fourth the number of digits after the decimal separator has been changed.</em></p>
<h3 id="heading-how-to-calculate-a-percentage-of-multiple-percentages-in-excel">How to Calculate a Percentage of Multiple Percentages in Excel</h3>
<p>Let's take the example used above.</p>
<p>An online store has a promotion on some items for 20% off. Then there is another promotion of 10% off for everything in the cart at the moment of payment.</p>
<p>The first discount means you pay 80% of the original price of the item. And then you pay 90% of what's in the cart.</p>
<p>How much is the discount?</p>
<p>You get the result by multiplying the two percentages. ( 90\% \cdot 80\%).</p>
<p>In Excel, if you have the first percentage in I2 and the second in J2, you can write <code>=I2*J2</code> to calculate the result.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/08/image-120.png" alt="Image" width="600" height="400" loading="lazy">
<em>In the first image you can see the formula to calculate the percentage from multiple percentages, and then its result in the second image.</em></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Percentages are a mathematical tool we use in every day life. In this tutorial, you have learned how they work mathematically, and how to use them in Excel.</p>
<p>I hope this article has been useful.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
