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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Knowing regular expressions, or simply regex, is very useful, but it tends to look a bit scary to a beginner. In this article we cover the basics of regex using Python
You may have been in this situation: you are writing a paper or doing research and find a figure that presents interesting data, but it is low quality or you would like to add or remove elements from it. However, the data that originated it are not available. How do you get out of this? That is what we will see in today’s article.
Usain Bolt is considered by many to be the greatest sprinter of all time. In this article, we will see how to use Python to find the speed and acceleration the Jamaican reached in his races. We will also take the chance to discuss mathematical models and the care needed in choosing them and interpreting their results.
In this article we detail how to get visually elegant plots that correctly show units on their axes. We will combine the pint, numpy and matplotlib libraries.
Whenever we work with measurements and experimental data, we need to express our values with their respective units. In this article, we will see a Python library for working with units and what that has to do with beer.
In this article we will see how to use virtual environments to manage Python projects. We will see that it is possible to have a different Python version for each project, and also how packages and dependencies are controlled.
You have probably heard of data science or machine learning. And you may also have heard of, or worked with, the R or Python languages. Today’s article is about Anaconda, a distribution that aims to make it easier to install and manage tools for these two languages, especially for scientific applications.