11 Ways to Do Math on the Linux Terminal


Would you like a quick, distraction-free way to solve your math problems the way scientists and engineers do? Many tools let you do math right in the Linux terminal. You can tackle easy problems and make hard ones possible with these programs.

11

Bash Arithmetic Operators

Did you ever need to do arithmetic while you were working in Bash? You can use Bash arithmetic operators. Like everything else with Bash, the syntax may look ugly but it will get the job done.

Bash arithmetic operations in the terminal.

In Bash, arithmetic operations are enclosed in $(( )) blocks, or a dollar sign followed by two open parentheses, and closed by two closed parentheses. The arithmetic operators are similar to the ones you might find on a hand calculator or in a spreadsheet program.

For example, to add two numbers and print them to the terminal

        
echo $((2 + 2))

I told you it was ugly. It should print the number 4 to the terminal.

Similarly, to subtract two numbers;

        
echo $((3 - 5))

The answer will be -2. Multiplication uses the * operator:

        echo $((42 * 25))
    

The exponent operator is ** (two asterisks). You can remember this by the fact that exponentiation is multiplying a number by itself:

        echo $((7**2))

Division uses the / (slash) operator. It returns the quotient, but not the remainder, which is given with the modulo (%) operator. So $((5 / 2)) will return 2, while $((5 % 2) will return 1, which is the remainder of 5 divided by 2. The downside of Bash arithmetic is that it only does integers.

Bash arithmetic might be better suited to scripts, but it works and is available on most Linux systems.

10

Python

If Bash math is ugly, Python might be more palatable. You don’t need to preface your calculations with $((, after all. Using Python is much simpler.

Python math operations in the terminal.

Just type “python” at the Linux prompt. If you have iPython installed, you can use that instead. With the interactive prompt running, you can use the standard operators.

For example, to add:

        2 + 2
    

And to subtract:

        5 - 3
    

To multiply:

        123 * 456
    

And to divide:

        393039 / 3292
    

Python arithmetic also happens to work in floating-point, which means you can use decimals right away.

The exponent operator is the same as the one for Bash:

        2**8
    

This returns the number 256.

The arithmetic options in Python’s interactive mode are so easy it’s a common joke among Python programmers that Python makes a great desk calculator, something that’s acknowledged in the official Python calculator tutorial.

While you can use Python for simple arithmetic, its libraries add to its power. You can use many libraries to tackle everything from statistics to calculus.

Related


8 Tips and Tricks for Using Python as a Calculator App

Throw away your fancy calculator and pop open a Python window instead!

These libraries and Python’s simplicity make it a language of choice for science, and you can have these same tools right on your desktop.

9

SageMath

One of my favorite terminal math tools is SageMath. It’s intended to be an open-source answer to major proprietary packages like MATLAB and Mathematica which are widely used in academia and industry. It’s useful if you’re studying advanced math or want to play around with a powerful computation system.

SageMath is based on Python but uses several other open-source projects and libraries, including other ones that are mentioned in this article.

SageMath is a computer algebra system, which means that it operates on symbolic math like a hand calculator operates on numbers. If you take a square root, it will evaluate it symbolically:

        
sqrt(42)

Sage will display 42 inside the radical symbol because it’s not a perfect square. If we take the square root of 180:

        
sqrt(180)

It will automatically factor out 6 because 6 squared is 36 and 36 * 5 is 180, so the symbolic result is 6 times the square root of 5.

SageMath square root calculations in the terminal.

You can get a decimal approximation similar to what you would get with a scientific calculator with the n() function:

        
n(sqrt(180))

You can also select the number of digits you want for the precision with the digits option:

        
n(sqrt(180),digits=5)

Of course, you’ll probably want to do more than just take square roots.

You can use SageMath as a calculator as with Python and Bash, but you can do some cool things like solve equations. To solve 5x + 3 – 15, use the solve function to solve for x:

        
solve(5*x + 3 == 15,x)

Sage will return the answer 12/5. It seems to like improper fractions.

You can also solve quadratic equations

        
solve(2*x^2 + 3*x - 5,x)
Solving linear and quaradtic equations in SageMath in the terminal.

Polynomials of higher degrees work the same way.

Let’s solve a system of linear equations. You could use the solve function, but you’d have to write out the equations in full. Using a matrix means less typing. Here’s how you do it in SageMath. My favorite example is the first one you see on the Wikipedia page for systems of linear equations:

3x + 2y – z = 1

2x -2y + 4z = -2

-x + 1/2y – z = 0

We’ll define the matrix of coefficients using the matrix command:

        
A = matrix([[3,2,-1],[2,-2,4],[-1,1/2,-1]]

Then we’ll define the column vector:

        
 b = vector([1,-2,0])

And then we’ll solve it:

        
A.solve_right(b)
Solving a system of linear equations with a matrix in SageMath in the terminal.

There’s so much more than I can cover here. Since SageMath is based on Python, you can use other Python libraries with a powerful computer algebra system. You can have the functionality of a graphing calculator for free and on a much better screen.

8

Maxima

Maxima is another similar computer algebra system to SageMath. It’s based on the much older Macsyma system dating back to the 1960s at MIT. While Symbolics commercialized Maxima in the 1980s, the US Department of Energy licensed the original for academic and government use.

Maxima session in the terminal.

This version would eventually be open-sourced and renamed Maxima. Maxima has a small following in academia for mathematical and scientific calculations. SageMath even uses it for some functions.

Operations are similar to SageMath. Statements are terminated by a semicolon, which can be annoying if you forget it. I have to keep remembering to put it at the end of statements.

Taking the square root of a number is easy:

        sqrt(4);
    

You can also solve equations with Maxima, similar to SageMath:

        solve(5*x + 3 = 15,x);
    

7

R

R is a popular language for statistical analysis and data science. While there are graphical IDE-like programs you can use with it, it will also happily run in the terminal. You can do simple descriptive statistics like means, medians, and standard deviations and move to linear regressions and statistical tests like Student’s t, Chi-square, and ANOVA.

Here are a few simple descriptive statistics:

Let’s define a small dataset. A list in R is called a vector:

        a  23, 45,25)
    

Let’s take the mean of the vector a:

        mean(a)
    

And the median:

        median(a)
    
Calculating the mean and median of a vector in R in the terminal.

6

sc-im

sc-im spreadsheet in the terminal.

sc-im is a spreadsheet application that runs in the terminal. If you ever used Lotus 1-2-3 back in the ’80s, the layout might look familiar to you. It’s based on the original sc developed by Java creator James Gosling. It aims to add a bunch of improvements, similar to Vim’s relationship to Vi.

With sc-im, you can go back in time and run the numbers in text style and imagine you’re a Wall Street trader.

You move through sc-im similarly to Vim. To input a numerical value, press the = key.

To sum a series of cells, you use the sum function:

=sum(A0:A4)

5

bc

bc calculations in the terminal.

bc is a command-line calculator that stands for “Basic Calculator.” It has all of the basic operations you would expect on a simple calculator but without the annoying syntax of Bash. Like Bash, bc is part of the GNU project.

In contrast to dc, bc works in a more conventional infix notation mode. For example, 2 + 2 is just 2 + 2, the way most people learn how to do it in school.

4

dc

dc calculations in the terminal.

dc is a very old Unix utility, but it’s also widely available on modern Linux systems. It uses a form of input called Reverse Polish Notation or RPN, which was dominant on the HP calculators that scientists and engineers in the 1970s and 1980s used.

The key to RPN is the stack. You push numbers onto the stack as if they spring-loaded plates in a cafeteria well, and pop them to perform operations. To calculate 23 + 45, you push both numbers onto the stack, and the “+” operator pops them and pushes the result back onto the stack.

This is how it looks like in the terminal

23
45
+

You use the “p” command to print the result to the terminal.

3

Qalculate!/Qalc

qalc calculations

Qalculate! is well-known as a powerful calculator with lots of advanced math options, but it also has a command-line variant, known as qalc. All of the standard operations are there, but it also has some powerful operations similar to what I’ve shown in other CAS programs, including calculus.

I like the way it highlights the results in the terminal with color. I think it’s a nice touch and makes the results stand out on the screen.

If you’re a real calculator nerd, you can even set it to RPN mode like in bc. The Qalculate features list is a math and science nerd’s dream.

2

GNU Octave

GNU Octave is a clone of MATLAB, it’s designed to work with the matrices and vectors of linear algebra widely used in all sorts of scientific and engineering calculations. You can have the same power without the hefty licensing fee.

Here’s the same system we solved earlier solved Octave-style by multiplying the inverse of the matrix by the column vector:

A = [3 2 -1; 2 -2 4; -1 1/2 -1]
b = [1; -2; 0;]
A \ b
Solving a system of linear equations with GNU Octave in the terminal.

1

Axiom

Axiom session in ther terminal.

Like Maxima, Axiom is a computer algebra system with a long history. The ideas behind Axiom were originally developed by IBM back in the 1960s. It was also released as a commercial product that was eventually open-sourced.

While Axiom runs its commands in the terminal, when it opens up you get a decidedly graphical help menu that shows all of the functions.

As with SageMath and Maxima, you can bring the power of scientific computing down to your machine, tackling anything from solving simple equations to calculus and linear algebra. The solve function is similar to what we’ve seen earlier.

solve(5*x + 3 = 15,x)

There’s a reason that Linux is the OS of choice for the scientific community. There are lots of tools for researchers to get their work done, and you can start exploring the fascinating worlds of math and science right from your terminal as well with these tools and the many more you might discover in your package manager.



Source link

Previous articleBattered BTC Market Looks to Federal Reserve for Support, BofA Predicts End of QT
Next articleHow Linux Took Over the World (Without Anyone Noticing)