15

Computers are an exceptionally powerful tool for various computations, but they don't excel at storing decimal numbers. However, people have managed to overcome these issues: not storing the number in a decimal format, which is limited to very few decimal places, but as an integer instead, while keeping track of the number's precision.

Still, how can a computer simplify computations just like humans do? Take a look at this basic example

$$\sqrt{3} \times (\frac{4}{\sqrt{3}} - \sqrt{3}) = \sqrt{3} \times \frac{4}{\sqrt{3}} - \sqrt{3} \times \sqrt{3} = 4 - 3 = 1$$

That's how a human would solve it. Meanwhile, a computer would have a fun time calculating the square root of 3, diving 4 by it, subtracting the square root of 3 from the result and multiplying everything again by the square root of 3.

It would surely defeat a human in terms of speed, but it would lack in terms of accuracy. The result will be really close to 1, but not 1 exactly. A computer has no idea that, for instance, $\sqrt{3} \times{\sqrt{3}}$ is equal to $3$. This is only one of the uncountable examples out there.

Did people already find a solution, as it seems elementary for mathematics and computations? If they didn't, is this because it didn't serve any purpose in the real world?

Robert
  • 185
  • 1
  • 5

7 Answers7

38

Sage is an open source computer algebra system. Let's see if it can handle your basic example:

sage: sqrt(3) * (4/sqrt(3) - sqrt(3))
1

What is happening under the hood? Sage is storing everything as a symbolic expression, which it is able to manipulate and simplify using some basic rules.

Here is another example:

sage: 1 + exp(pi*i)
0

So sage can also handle complex numbers.

Computers never handle real numbers, since real numbers cannot be represented exactly on a computer. Instead, they either handle approximate representations of real numbers (usually floating point numbers but sometimes fixed point numbers), or they represent real numbers symbolically, as in the example above. Sage can convert between the two representations (in one direction!), and it can handle floating point numbers of arbitrary accuracy. For example,

sage: RealField(100)(pi^2/6 - sum(1/n^2 for n in range(1,10001)))
0.000099995000166666666333333336072

This computes $\pi^2/6 - \sum_{n=1}^{10^4} 1/n^2$ to 100 bits of accuracy (in the mantissa).

Another approach worth mentioning is interval arithmetic, which is a way of computing expressions with a guaranteed level of accuracy, using provable error brackets. Interval arithmetic is used in computational geometry, together with exact representation of rational numbers.

In theoretical computer science there are several other notions of real computation, but they are mostly of theoretical interest. See the answers to this question.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
19

Computer algebra is a huge area, probably at least a semester-long university-level course to get most of the basics. However, I think we can cover some of the flavour of it here.

Your case is the easy case, because it is entirely within the language of algebraic numbers (i.e. roots of polynomials), and manipulating polynomials is really the foundation of modern computer algebra. Specifically, you need to understand the concept of a field extension.

A field, you will recall, is an algebraic structure which supports addition, subtraction, multiplication, and division.

Starting with the rational numbers $\mathbb{Q}$, we can extend this with a monomial, which we will call $x$. This gives us the field $\mathbb{Q}[x]$, where the elements of the field are rational polynomials:

$$\frac{p(x)}{q(x)}$$

where $p$ and $q$ are polynomials whose coefficients are rational numbers. We can go further and extend with more monomials, to form, say, $\mathbb{Q}[x][t]$, whose elements are rational polynomials $\frac{p(t)}{q(t)}$ where the coefficients are in $\mathbb{Q}[x]$.

This gives us a language in which we can understand algebraic numbers such as $\sqrt{3}$. It is essentially a field extension with a constraint: $\mathbb{Q}[x]$ subject to the constraint that $x^2 - 3 = 0$.

(NOTE: I'm ignoring the negative solution for the moment, but note that it's not necessary to solve your specific problem! Which solution you use makes a difference when you introduce inequalities. Without knowing that $x > 0$, $\sqrt{3}$ and $-\sqrt{3}$ are surprisingly difficult to tell apart, but that's a tale for another time.)

Technically, this is called an "ideal" rather than a "constraint". And we denote this field as a quotient, $\mathbb{Q}[x] / (x^2-3)$.

Using this machinery, simplifying equations in this form follows a mechanical procedure:

  • Express the equation as an element of the appropriate field extension, applying ideals as you proceed.
  • If the element is a fraction, reduce it using Euclid's greatest common divisor algorithm.

We can start with your equation and express it as a rational polynomial. Perhaps we use a naive method and get something like this:

$$x \times \left(\frac{4}{x} - x \right) = \frac{x}{1} \times \left( \frac {4 - x^2}{x} \right) = \frac{-x^3 + 4x}{x}$$

We can eliminate the $x^3$ by dividing the numerator by $x^2-3$ and taking the remainder. To see why this works, take $x^2 - 3 = 0$ and multiply both sides by $x$, giving $x^3 - 3x = 0$. Add that to the numerator to get:

$$\frac{-x^3 + 4x}{x} = \frac{x}{x}$$

In general, if the ideal has degree $2$, we should be able to reduce any polynomial to a polynomial of degree $1$.

Finally, we can apply Euclid's GCD algorithm to the numerator and denominator to reduce this fraction to $\frac{1}{1}$. And the problem is solved.

EDIT Having said all that, do keep in mind that it's not always obvious what it means to "simplify" a mathematical expression. In some circumstances, a polynomial may be "simpler" if it's expanded and in others, it may be "simpler" if it's factorised.

Pseudonym
  • 24,523
  • 3
  • 48
  • 99
5

Here is your mistake:

That's how a human would solve it. Meanwhile, a computer would have a fun time calculating the square root of 3, diving 4 by it, subtracting the square root of 3 from the result and multiplying everything again by the square root of 3.

You are assigning a mind to a computer. That's the analog of writing "that's how a gas stove would cook a fish curry". The stove, like the computer, provides mechanisms, like floating point arithmetic. Well, the gas stove actually is better at providing heat than the computer (though it depends on the computer). And either computer or stove certainly come in handy as tools in the process for solving a particular problem. But they are not responsible for the solution.

What is involved here is symbolic computation. You can easily write specific programs for solving particular classes of symbolic computation problems, and there are generic programs for doing that, too. Yes, computers are excellent tools for arriving at numerical solutions, but you can also create numerical solutions using pencil and paper (and many computer arithmetic methods are distilled from pencil-and-paper methods, sometimes after converting to binary arithmetic). Or slide rules.

When a computer produces symbolic output, like a human does when writing down symbols on paper, the scope and meaning of the produced symbols depends on the program that is being used for creating them. Some programs will just produce numerical results (which are easily converted into symbols using digits and a few special characters). Others are intended to produce symbolic results where feasible. Being a computer does not really play a whole lot into it except that humans tend to be a lot better at finding excuses for avoiding calculations (since they are so bad at calculating) and a lot of problems given to humans for practice thus tend to magically have solutions that are simpler than actual real-world problems.

2

Even for numeric, seemingly obvious computations, computers can struggle: most will have trouble simplifying expressions such as $0.1 + 0.2 - 0.3$.

The following paper gives another way of encoding real numbers, for numeric computations, but without those rounding problems:

Towards an API for the real numbers, H.J. Boehm (2020).

The idea is to represent each number as a function computing its decimal expansion (or a rational approximation with bounded error): common arithmetic operations then just combine those functions into other functions.

It is apparently used in the calculator app on Android phones.

VDZ
  • 21
  • 2
2

First of all, you are mixing different concepts. In your example, you are using algebraic numbers. There algebraic numbers are countably infinite, while real numbers are uncountably infinite.

Generally, for every countably infinite set you can find a data structure that can represent each element of the set (size constraints notwithstanding).

Thus, if you want to simplify expressions of algebraic numbers you can find a way to represent algebraic numbers without loss and then apply a solving algorithm.

√3×√3 is equal to 3. This is only one of the uncountable examples out there.

Again, there are only countably infinite examples. That's why it can work.

For the full set of real numbers, this would not be possible.

Cephalopod
  • 133
  • 6
1

You are mistaking a map for the territory.

The first thing to realize is that computers don't work on numbers. Computers work on bits and volts. We abstract that work on volts into working on bits, and we abstract the work on bits to be working on numbers.

Most modern computers use IEEE floating point numbers, not because that is the only thing that they can use, but because IEEE floating point numbers are useful to solve certain kinds of problems. So special purpose hardware to make working with IEEE floating point numbers is added to computers, and they can do calculations using them that are extremely fast.

IEEE floating point numbers represent values as an integer times 2 to the power of another integer, essentially. Both the integer and the power are fixed precision -- they have a max and min size.

When you represent the square root of 3 (or even 0.1) as an IEEE floating point number, you end up with an approximation of the square root of 3. There is some error.

As this error is small, and by design each operation has a reasonably random rounding error at the scales we typically work with, you a likely to get decent approximations of the result of most calculations. But not all calculations.

In other domains, instead of IEEE floating point numbers computers use integers with an upper bound, or integers with no upper bound. These are all common ways to do math on a computer.

There is nothing forcing computers to use IEEE floating point values, or integers, or whatever. Computers are capable of doing symbolic manipulation.

They can represent $x^2=2$ as exactly that -- a string of characters, or tokenize it and turn it into a representation of the algebraic structure. They can do various things we teach high school and undergraduate students to do. They can go through everything you described a "human mathematician" doing to simplify the equation.

They can even do this faster than human mathematicians can do quite often.

This area is called "computer algebra".

In my experience, the hard part is usually explaining the problem to the computer. When you see $x^2=-2$ you probably assume we are working in the reals or the complex numbers, but not the quaternions, constructive reals, $\mathbb{Z}_2$ or something more exotic.

$x^2=2$ in $\mathbb{Z}_2$ means $x=0$. In $\mathbb{R}$ it is unsolvable. In $\mathbb{C}$ it is plus or minus the $i \sqrt{2}$. Etc.

Going further, the constructive reals are a way of axiomitizing real numbers that basically relies on algorithms and proofs.

In it, $\sqrt{2}$ "becomes" a series of values ${v_i}$ and a (provably correct) algorithm that takes any $\epsilon \in \mathbb{Q}$ that is greater than zero, and produces an $N$ such that for all $i>N$, $|v_i^2 - 2| < \epsilon$.

Using this you can produce $\sqrt{2}+\sqrt{3}$ by composing the two series with the algorithm. Even $\sqrt{\frac{2}{3}}$ can be done similarly using nothing but mechanics.

As it happens, $\sqrt{2}-\sqrt{2}=0$ cannot be done as simply; and, in fact, you need to know more about $\sqrt{2}$ than just the series and a black box copy of the algorithm to show that two instances of it when subtracted are zero. In constructive mathematics, equality is not something you get for free, because there is no general way in the real world to prove if two different numbers are equal or not.

The same is true in human mathematics; there are things that are probably provably zero, or equal, we cannot prove are zero.

Yakk
  • 852
  • 4
  • 13
-1

Most people use a compiler, which creates operations with floating point numbers (which are not real numbers).

There are specialised tools that will try to manipulate formulas in symbolic form, and they can do exactly what a human can do. You can even do it yourself to sum degree, in any OOP language you can create objects that represent a number as the sum of rational numbers times square roots of integers for example, implement +, -, *, / and you will get exactly what you are asking for.

In principle, question like yours can always be answered by saying "either the designer of the processor adds hardware that does what you want, or some software developer creates software to do it". (Or nobody is bothered doing it, sometimes because it is too difficult, and in some cases it's impossible, but those impossible problems are impossible for humans as well).

gnasher729
  • 32,238
  • 36
  • 56