Differentiating and setting the result to zero solves textbook optimization. With several variables, constraints, or a function without a tractable derivative, that calculation breaks down. We will see minimize, SLSQP and bounds from scipy.optimize, local minima, and global search with differential_evolution.
The classic pendulum period formula only holds for small oscillations, and the exact equation has no closed-form solution. With SciPy’s solve_ivp, we measure the real period and solve stiff systems, Lotka-Volterra, falling with drag, and consecutive reactions.
The Gaussian function has no elementary antiderivative, and that is a theorem of Liouville. When the antiderivative does not exist, or all you have is a table of measurements, the way forward is numerical: quad, trapezoid and Simpson, from scipy.integrate, with the care each one demands.
Is the reactor temperature rising too fast? The question is a derivative, but all we have are numbers in a table. We will see finite differences, numpy.gradient and scipy.differentiate, and why shrinking the step does not always improve the derivative, because of rounding and noise.
Experimental data rarely falls on a curve. We will see how NumPy fits polynomials to noisy data, what SymPy cannot do, why extrapolating is dangerous, and why Runge’s phenomenon comes from equally spaced nodes, not from high degree.
Stored in float64, the coefficients of Wilkinson’s polynomial no longer return the roots 1 to 20: two of them become a complex pair. Before getting there, we look at roots, derivatives, integrals and division with numpy.polynomial, and at the coefficient-order trap.
From the pH of a weak acid to the volume of a real gas and the friction factor of a pipe: three equations that chemistry and engineering use every day and that only have a useful numerical solution. We solve all three with brentq and numpy.roots and rebuild the Moody chart.
Where is Halley’s Comet on any given date? The answer requires solving Kepler’s equation, which has no closed form. With SciPy’s newton and brentq, we locate the comet, make the law of areas visible, and compute the range of a projectile with air resistance.
In Kepler’s equation, the unknown appears both inside and outside a sine, and no algebra isolates it. For cases like this, SciPy approximates the root. We will see bisection and brentq, Newton’s method and why it can fail, and nonlinear systems with fsolve and root.
Most equations, integrals, and differential equations in real problems have no closed-form solution, and SymPy can’t solve them. This 13-article series shows how NumPy and SciPy tackle these problems: roots, polynomials, numerical calculus, optimization, and FFT.
A one-way street, a hyperlink, and a dependency between tasks are relations with a direction, and directed graphs exist for them. We will see in- and out-degrees, strong, unilateral, and weak connectivity, matrices, DAGs, and the topological sort that Apache Airflow uses.
Try to connect three houses to gas, water, and electricity without crossing lines: it is impossible. We will see why, with Euler’s Formula, the proof that the three-houses graph and the complete graph on five vertices are not planar, Kuratowski’s Theorem, and the coloring of planar graphs, from six colors down to four.
Traversing each edge exactly once is easy to decide: just look at the parity of the degrees (Euler’s Theorem). Deciding whether we can visit each vertex exactly once is NP-complete. We will see both problems, the theorems of Dirac and Ore, and the Traveling Salesman, from Held-Karp to Christofides.
A file system is a tree: a connected graph with no cycles. We prove that every tree has one edge fewer than it has vertices and that its center has one or two vertices, and we look at spanning trees and rooted trees, including the difference between a balanced tree and a degenerate one.
How do we know whether two points of a network are connected, and by which shortest path? We define walk, trail, path, and cycle, connectivity and distance, compute distances with breadth-first search and Dijkstra, and prove that a graph is bipartite if and only if it has no odd cycle.
Two graphs with the same number of vertices, the same number of edges and the same degree sequence may not be isomorphic. We will see what isomorphism is, how to prove it with a bijection, and how to represent graphs by adjacency and incidence matrices.
The sum of the degrees of a graph is always twice the number of edges: this is the Handshaking Theorem. We will see degree, regular graphs, and the degree sequence, and we check the theorem on Zachary’s Karate Club, a real network of 34 people that split in two.
The formal vocabulary of graph theory: vertices, edges, neighborhood, isolated and universal vertices, complement, induced and spanning subgraphs, complete and null graphs, cliques and independent sets, with 14 worked examples and exercises.
Can you cross the seven bridges of Königsberg passing over each one exactly once? Euler showed you can’t, and in doing so created graph theory. We will see what a graph is, the Four Color Theorem, and the computation of PageRank on a mini-web of five pages, in Python.
In 1736, Euler solved a puzzle about the bridges of a Prussian city and, without meaning to, founded graph theory. This series of 9 articles goes from the basic definitions to Kuratowski’s Theorem, through paths, trees, planarity, and directed graphs.
What is the coefficient of a term in the expansion of a binomial raised to the tenth power? The binomial theorem answers in one line, with the coefficients from Pascal’s triangle. We will see the theorem and its proof by induction, the general term formula, and the identities that come from special cases.
A set with 10 elements has 1,024 subsets, and that is the sum of row 10 of Pascal’s triangle. We will see the identities of binomial coefficients: symmetry, Pascal’s rule (Stifel’s rule), and the row, column, and diagonal theorems.
At an ice cream shop with 7 flavors, there are 210 ways to order 4 scoops, repeating flavors allowed. We get there with the stars and bars method, which shows that choosing with repetition, distributing balls into boxes, and solving integer equations are the same problem.
A 4-digit password allows repetition, and that is why there are 10,000 of them: ten options in each of the four positions. The arrangement with repetition counts passwords, license plates, and RGB colors, and shows how many patterns fit in the 32 bits of a float and in the registers of an HP-11C calculator.
How many anagrams does the word MAMBEMBE have? Fewer than an 8-letter word with distinct letters would have, because swapping two equal letters does not create a new anagram. The formula discounts the repetitions by dividing by factorials, and it works for binary sequences and for shortest paths on grids.
Out of 10 friends, 3 go buy the popcorn. Only who goes matters, not the order of the choice, so there are 120 possible groups. We will see the combination formula, its symmetry, the complement method, and how to split a set into groups.
In a race with 10 athletes, the podium can be filled in 720 ways. That is an arrangement, an ordered selection of some elements of a set. We solve problems about routes, license plates, and numbers with distinct digits, with Python and SQL.
How many anagrams does the word VIRUS have? In how many ways can 5 friends sit around a table? The answers are 120 and 24, by simple and circular permutations. We will also see adjacency restrictions, the traveling salesman problem, and derangements.
With 4 math books and 3 Portuguese books, there are 7 ways to pick one book and 12 ways to pick one of each. The first count is a sum and the second is a product: these are the sum rule and the product rule, applied here to passwords, flags, answer keys, and counting divisors.
How many 8-character passwords are there? In how many ways can 10 people sit around a table? Combinatorics answers without listing every case. This is the roadmap of the series: from the counting rules to permutations, arrangements, combinations and the binomial theorem.
How many moves does it take to solve the Tower of Hanoi? The answer comes from a recurrence relation, which writes the problem in terms of itself with one disk fewer. We set up recurrences and solve them by substitution, by the characteristic equation and by the Master Theorem, used for merge sort.
What if the step depends on more than one earlier case, as in Fibonacci? Strong induction assumes all the earlier cases. We will see the technique at work on prime factorization, the chocolate bar, postage stamps and Binet’s formula, which in floating point fails from the 71st term on.
If the first domino falls and each domino knocks over the next, they all fall. That is the logic of the Principle of Mathematical Induction. We will see the structure of the proof (base case, hypothesis and step) in sums, divisibility and inequalities, and the false proof that all horses are the same color.
How do you prove a formula holds for every natural number, if there are infinitely many of them? With mathematical induction. This introduction maps out the series: classic induction, strong induction and recurrence relations, from Fibonacci to the Tower of Hanoi.
Testing whether an element is in a set takes constant time on average, while in a list the time grows with its size. We will see how set theory becomes Python code: set and frozenset, comprehension, membership and inclusion, union, intersection and difference, hashing, and practical applications.
Adding the sizes of two sets counts the intersection twice. The Inclusion-Exclusion Principle fixes the count, for two, three, or any number of sets. We will also see infinities of different sizes, derangements, and the Sieve of Eratosthenes.
Union, intersection, difference, and complement are the basic operations on sets, and they show up in SQL as UNION, INTERSECT, and EXCEPT. We see each one in Venn diagrams, along with De Morgan’s laws, distributivity, and the difference between a Venn diagram and an Euler diagram.
The collection of tall people is not a set: there is no criterion that decides, for any person, whether they belong or not. We define set, membership, roster and set-builder descriptions, equality, inclusion, and the power set.
In how many ways can you choose a valid password? What is the shortest path between two cities? These are questions of discrete mathematics. This roadmap introduces the site’s four series on the topic: sets, induction and recurrence, combinatorics, and graphs, with 28 articles.
How do we go from a truth table to the leanest Boolean expression? We show three paths: the canonical forms with minterms and maxterms, algebraic simplification, and the Karnaugh map, with worked exercises and a check in Python with SymPy.
Every operation of a processor can be described with three logical operations: AND, OR and NOT. We go from gates and truth tables to theorems, De Morgan’s laws and the simplification of expressions, with the half adder as the first circuit and examples in Python.
With 10% serial code, eight cores make the program only 4.7 times faster, and no number of cores gets past 10 times. We close the series with RISC and CISC, performance metrics, Flynn’s taxonomy, SMP, clusters, NUMA, multicore, and Amdahl’s Law.
Busy-waiting on a 10 ms disk, a 1 GHz CPU wastes ten million cycles. We compare the three ways of handling input and output (polling, interrupts, and DMA), calculate the overhead of each one, and see why polling came back with NVMe SSDs.
Python does not run your code line by line: first, CPython compiles it to bytecode. With the tokenize, ast, compile, and dis modules, we follow a simple expression from tokens to syntax tree to bytecode, right in the interpreter.
You write a line of code, and the processor only understands bits. We follow the translation: the two-pass assembler, the compiler phases, linking and loading. Along the way, GCC cuts twelve instructions down to two with -O2, and we see where Python and Java sit between compiling and interpreting.
The same expression costs 5 instructions with three operands, 7 with two, and 11 with an accumulator. We will see the ISA, the contract between hardware and software, instruction formats, the RISC-V R-type, and the seven addressing modes that tell where each operand is.
Uppercase and lowercase differ by a single bit in the ASCII table, and that was no accident. To the hardware everything is bits, and the value depends on the reading convention: ASCII, Unicode, and UTF-8 for text, two’s complement for integers, and IEEE 754 for real numbers.
An assembly line does not wait for one car to be finished before starting the next, and the pipeline does the same with instructions. We will see the stages, the hazards that stall the line, the speedup calculation, the buses that connect CPU, memory, and peripherals, and the control unit.
A hypothetical 12-bit processor with 15 instructions serves as a model for following the complete instruction cycle. We execute LDA and ADD register by register, encode instructions in hexadecimal, and write assembly programs with branches and loops.
A 2 GHz processor can execute more instructions per second than a 3 GHz one. To understand why, we open up the CPU: ALU, control unit, registers, flags, decoder, and clock, along with the microevents of the fetch cycle in formal notation.
With a 95% hit rate, the cache makes the processor see a 4.95 ns memory where DRAM takes 80 ns. We will see why it works (locality), the direct, associative, and set-associative mappings, the replacement algorithms, and the write policies.
If a register access took one second, a main memory access would take four and a half minutes, and a hard disk access, more than a year. We will see the memory hierarchy, cells, addresses and buses, the sizing formulas, and SRAM, DRAM, ROM and ECC.
You press Enter and the result shows up three tenths of a second later. In that interval, the processor fetched hundreds of millions of instructions from memory. This series of 13 articles explains how: memory, cache, CPU, pipeline, data representation, and program execution.
How can the same 32 bits hold the distance between galaxies and the diameter of an atom? We dissect IEEE 754: sign, biased exponent and significand with a hidden bit, hand conversions, infinity, NaN, subnormals and machine epsilon.
Why doesn’t 0.1 plus 0.2 give 0.3? Because 0.1 is a repeating fraction in binary. We convert fractions between bases by successive multiplications, look at the bits of a float with struct, and see two practical ways out: integers for money and math.isclose for comparisons.
In Python, 5 and 6 returns 6, and 5 & 6 returns 4. We explain the difference and the bitwise operators AND, OR, XOR, NOT and the shifts, used in masks that check, set, clear and toggle bits. In the end, we see where the 755 in chmod comes from.
To subtract, the processor adds the first number to the complement of the second, so a single adder does both operations. We will see two’s complement, why 8 signed bits range from -128 to +127, and how overflow took down the Ariane 5 and froze Pac-Man on level 256.
The addition and subtraction rules of base 10 hold in any base: only the value that triggers the carry changes. We do arithmetic in binary, octal, and hexadecimal, see why with three addends the carry can be worth 2, and implement both algorithms in Python.
In any base, the value of the base itself is written 10. We start from positional notation to convert integers between decimal, binary, octal, and hexadecimal, using expansion, successive divisions, bit grouping, and Python’s bin, oct, hex, and int functions.
Both NumPy and pandas use the concept of an axis, but with subtle differences. In NumPy, it refers to the dimension along which an operation is performed, while in pandas it indicates the direction of an operation. This article presents examples of using axis in each library.
We learn in math that there are rules for adding or multiplying matrices. But in computational math problems we sometimes have matrices or vectors that we would like to add or multiply and that do not meet the requirements. In this article we will see how the NumPy broadcasting concept is useful in these contexts and how it relates to vector quantization.
If you are venturing into data science, you have most likely seen a reshape in some code. Maybe with a mysterious value of -1 in one of the positions, as in reshape(-1, 1). But what does that mean? Why do we sometimes need to transform our data to use model methods in Scikit-Learn? That is what we will see in this article.
Monte Carlo methods are computational algorithms that rely on random samples to obtain numerical results. In this article, we will see how to obtain the value of pi with these methods.
The number π is a mathematical constant: the ratio of a circle’s circumference to its diameter. In this article we walk through the history of π and learn a bit of Python programming along the way.
A good part of the study of calculus is devoted to what are called infinite series. But what, exactly, are infinite series? And how are they related to the paradox of Achilles and the tortoise, one of the so-called paradoxes of Zeno of Elea, the Greek philosopher? What is the connection with quantum mechanics and the uncertainty principle? And where does Python come in? That is what we will find out in this article.
Mass balance equations are important for practically all calculations in solving chemical engineering problems. In this article, we will solve two mass balance problems using the SymPy library and review some important principles.
Did you know that Python’s max function has many options and ways of use that few people know about? In this post we will see different ways of using the max function in Python.
We have already written here on the site about the concepts of limit and derivative and how we can do the related computations with Python. Continuing our journey through calculus, in this article we will see how to work with integrals using SymPy and SciPy, Python packages. Along the way, we will pick up some notions of numerical methods.
I recently watched an excellent video about best practices in programming. But the idea can be broadened to best practices in general, for any career. And, deep down, it is a video about communication, as I will show in this article.
Engineers and scientists are always trying to get the most performance out of their equipment and projects at the lowest cost. This search gives rise to so-called optimization problems. In this article, we will see how to use Python, with the help of the SymPy package, to solve them.
Linux Mint is a friendly, easy-to-use Linux distribution. In this video (in Portuguese), we make a series of customizations that make it even friendlier and more productive.
We spend a considerable amount of time dealing with strings, so it is important to know how to present them the way we want. That is what we will see in this article, with plenty of examples.
The start of a calculus course tends to feel abstract because of the concept of a limit. But it soon becomes more tangible with the introduction of the derivative, which has many obvious practical applications. In this article, we will see how to work with derivatives using SymPy, a Python package.
The concept of a limit is the most important one in the study of calculus. Many other concepts and ideas are built on the definition of a limit. In this article, we will see how to find the limit of functions using SymPy and how this package can be a teaching aid in the study of calculus.
Complex numbers are fascinating and have many practical applications. In this article, we will see how Python handles complex numbers natively and how the SymPy package extends the power of the language.
Linux Mint is a distribution based on Ubuntu whose main desktop environment is Cinnamon. Its look is more familiar to people migrating from Windows, and it meets the needs of all kinds of users.
In this article, we explore 3 chemistry libraries available for the Python language, showing how they are useful in the daily routine of chemistry professionals and students.
What do ocean waves, soap bubbles, and DNA have in common? That is what I show in this article, with the help of Python. We will see how these subjects relate to trigonometric functions.
In programming, the word “logic” can refer to the process of designing an algorithm, to the conditional structures of the language, or to logical operators such as AND and OR. In the last two cases, we are within what is called mathematical logic.
Is it possible to study for the college entrance exam and still learn Python? Of course! In this article, we analyze a problem from the 2020 exam. It is a chemistry problem, but one that can be solved more with reasoning than with chemistry itself.
In this article we will see how to make plots with SymPy and Matplotlib. When people talk about SymPy, we usually focus on solving mathematical problems. But many forget that the library has some basic features for building plots, which we will see in this article.
A large part of fundamental math education is devoted to solving equations and systems of equations. In this article we will see how to use SymPy for these tasks quickly and intuitively.
Git can be a somewhat scary tool for beginners because of its large number of commands and possibilities. In this article, we demystify the main aspects of Git so that you can collaborate on any project. The famous workflow of forks and pull requests, popularized by GitHub, is covered.
Have you ever seen the term “yield from” in some Python code and wondered what it was? In this article we will dive even deeper into generators and understand, with examples, what “yield from” means and how we can use it to make our code even more efficient.
Did you know you can create an infinite sequence without running into memory problems and still consume that sequence however you like? Today we will look at a specific function from the itertools module, islice, which is excellent for our purpose. We will learn to use it with the example of the Fibonacci sequence.
In this article, we will see how the all and any functions, available in a standard installation of Python, work. We will also see what each of them is useful for and what to watch out for when using them, based on a complexity analysis.
Do you know the difference between a “normal” function and a generator function in Python? What is the difference between the return of a usual function and the yield of a generator? In this article we answer these questions and dig deeper into some aspects of the language. We will see how generators in Python can make your code up to 1000 times faster.
Do you know what an iterable is? And an iterator? How do you recognize these structures in Python? Answering these questions is the goal of this article.
The and and or operators are short-circuit operators, meaning that the second operand is only evaluated if the result cannot be determined from the value of the first operand alone. Confusing? Let’s look at some examples in this article.
The Olympics are coming to an end… and, besides nostalgia and stories of perseverance, every Olympics leaves behind an enormous amount of data! In this article, we will see how to use Pandas to analyze part of that data, looking at how athletes’ times have evolved in track and field events.
Is it possible to study for the college entrance exam and still learn Python? Of course! And that is one of the goals of the Brazilian Exam Problems category on this site. In it, we use computational tools to analyze and expand problems from the ENEM, Brazil’s national high school exam. In today’s article, we analyze a problem from the 2019 exam. And, along the way, we learn to make 3D and 4D(?!) plots with Matplotlib. All because of a killer brick.
Colorless, made of invisible constituents, and odorless (we hope!). We often forget that we are surrounded by the gases that make up our atmosphere. In this article, we will see how to easily get a list of the main constituents of air with Python and web scraping. We will also do some calculations, such as density and mean molar mass, and, of course, present everything in spectacular plots.
Let’s see how to structure a small Python project with the help of a very simple exercise. I will show the steps to set up whatever editor or IDE you use, how to work with version control (in this case Git), and how to develop with tests (TDD, Test Driven Development).
We will see the advantages of using an interactive Python interpreter over the language’s standard interpreter. IPython can help you study the language and produce Python-related content.